0

reading the ngrepeat source, I was wondering why it defines a compile function, while all that does is return a linking function, it's not manipulating the template passed into the compile function, as far as I can see.

4

2 回答 2

1

After looking at that source code link, it appears that the linking function does utilize a param from the compile function (the linker object). As to why it needs the linker obj, I'm not exactly sure.

于 2013-07-14T23:47:24.430 回答
0

Thats the way the Directive Definition Object is defined:

You could also do:

compile: function compile(tElement, tAttrs, transclude) {
      return {
        pre: function preLink(scope, iElement, iAttrs, controller) { ... },
        post: function postLink(scope, iElement, iAttrs, controller) { ... }
      }

Check Directives documentation at http://docs.angularjs.org/guide/directive

A compile function can have a return value which can be either a function or an object.

  • returning a (post-link) function - is equivalent to registering the linking function via the link property of the config object when the compile function is empty.
  • returning an object with function(s) registered via pre and post properties - allows you to control when a linking function should be called during the linking phase. See info about pre-linking and post-linking functions below.
于 2013-07-14T23:36:41.977 回答