11

In an angular directive's compile function there is a pre and post. Is this pre and post really the same as the link function?

For example in the code below, is the link function the same (shortcut if you will) as the pre and post of the compile function below it?

Link

....
link: {
  pre: function(scope, elem, attr) {
   //stuff
  },
  post: function(scope, elem, attr) {
  //stuff
  }    
}
....

Compile...

  ....
  compile: function(tElem, tAttrs){
    return {
      pre: function(scope, iElem, iAttrs){
      //stuff
      },
      post: function(scope, iElem, iAttrs){
      //stuff
      }
    }
  }
  .....
user1142130
  • 1,617
  • 3
  • 20
  • 34

2 Answers2

8

Compile is run first (and is usually where you maipulate your "template" dom elements). Link is run second, and is usually where you attach your directive to $scope.

They also run in a specific order, so you can use that fact when you're designing directives that require some "parent" directive setup in order to operate properly (like a tr:td sorta thing).

There's a really great article on timing for compile vs link you can take a look at for more clarity.

Also - there's a very low level stack answer to a similar question you might like (note that its NOT the one listed first, it's the one most upvoted).

So, What's the Difference?

So are compile pre/post link "the same" as the link function? You decide.

If you define compile on a directive, the framework ignores your link function (because the compile function is supposed to return pre/post link functions).

It's a little bit like link overloads compile.postLink and link.pre overloads compile.preLink.

Community
  • 1
  • 1
bri
  • 2,932
  • 16
  • 17
  • the article you posted is the one i was actually looking at when this question popped into my head. so i just need more of a "yes" or "no" to my question and my assumptions – user1142130 Jan 27 '16 at 14:24
  • so "is compile pre/post the same as link pre/post" ? Just a shortcut? no ;-) – bri Jan 27 '16 at 14:28
  • Fair enough. I'm just being lazy. Added to answer as it didn't really fit into a comment. – bri Jan 28 '16 at 04:13
  • When this overloading happens, are you aware of anything different happening (i.e. any other functionality being added) as supposed to just returning pre and post from compile? – user1142130 Jan 28 '16 at 19:33
0

When this overloading happens, are you aware of anything different happening (i.e. any other functionality being added) as supposed to just returning pre and post from compile?

If you look at the source code, when the $directiveProvider registers the directives, if the compile property is missing and the link property exists, it creates a compile property that is an empty function that returns the link property.

So the answer is that the link functions returned by the compile function are the same as the link functions provided by the link property of the DDO. No other functionality is added.

georgeawg
  • 48,608
  • 13
  • 72
  • 95