2

I find the source code of ngRepeat does not have compile option mentioned in the guide of HTML Compiler.

I'm puzzled about this....

Who can tell me why...

2
  • Miško Hevery, Igor Minár and Vojta Jína would probably know the best...
    – Shomz
    Jan 27, 2014 at 17:46
  • It has a link function, which is the result of the compile that happens on all directives. Jan 27, 2014 at 17:55

1 Answer 1

6

ngRepeat has a transclude option.

  • When using transclude , $compile compiles the transcluded element, and then provide a childTranscludeFn function to the linking function of the directive as the fifth argument (or $transclude in a directive's controller)
  • This function is used to create multiple clones of the same compiled template.
  • It is only compiled once when it's being transcluded.

A small code fragment from compile.js source code:

if (directiveValue = directive.transclude) {

  // some code

  if (directiveValue == 'element') {

    // some code

    childTranscludeFn = compile($template, transcludeFn, terminalPriority,
                                replaceDirective && replaceDirective.name, {
                                  // some comments
                                  nonTlbTranscludeDirective: nonTlbTranscludeDirective
                                });
  } else {
     // some code
     childTranscludeFn = compile($template, transcludeFn);
  }
}

inside ngRepeat:

  • This directive watches the collection for changes
  • On change, It iterates all collection items to find new added items (or removed items)
  • It clones the trancluded content to a new child scope for each new item (uses $transclude)

A fragment from ngRepeat source code:

var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
  var NG_REMOVED = '$$NG_REMOVED';
  var ngRepeatMinErr = minErr('ngRepeat');
  return {
    transclude: 'element',
    priority: 1000,
    terminal: true,
    $$tlb: true,
    link: function($scope, $element, $attr, ctrl, $transclude){

        // some code

        $scope.$watchCollection(rhs, function ngRepeatAction(collection){

          // some code

          for (index = 0, length = collectionKeys.length; index < length; index++) {

            // some code

            if (!block.scope) {
              $transclude(childScope, function(clone) {

                // some code

              });
            }
          }

          // some code
        });
    }
  };
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.