小编典典

AngularJS:了解递归指令

angularjs

我在这里找到了一个很棒的树指令。原文:http//jsfiddle.net/n8dPm/

我一直在试图通过其他几个做题,要了解它的功能。我不太明白呈现树指令的递归调用是如何工作的。主要是编译功能

  1. 什么时候所有的编译函数都调用?
  2. $ compile函数何时被缓存在变量中compiledContents(这是链接函数吗?),何时追加?为什么不总是附加?

-

compile: function(tElement, tAttr) {
            var contents = tElement.contents().remove();
            var compiledContents;
            return function(scope, iElement, iAttr) {
                if(!compiledContents) {
                    compiledContents = $compile(contents);
                }
                compiledContents(scope, function(clone, scope) {
                         iElement.append(clone); 
                });
            };
        },

阅读 274

收藏
2020-07-04

共1个答案

小编典典

Ng网站上有一些很棒的文档(我认为是最好的文档)。启动和运行时循环的概述非常有帮助:http
:
//docs.angularjs.org/guide/concepts

在较高的级别上,当Ng首次启动时,它将从ng-app所在的位置开始编译DOM(就像Ng的另一条指令一样)。这意味着它将遍历元素并查找链接至$
rootScope(编译/链接过程中属于原型继承链设置的所有作用域的根)所需的指令和表达式。如果是指令,则也将在其上执行编译过程。编译过程将采用在HTML中找到的所有Ng指令,并根据分配的优先级对它们进行优先级排序,或者假定优先级为零。当它们全部排序后,它将为返回链接功能的指令执行编译功能。在上面的示例中,有两个show链接函数,下面将对其进行注释以及将其链接到此说明的其他注释。

执行链接功能,链接功能将范围和指令链接在一起并生成视图。这可能包括HTML / transclude,因此可以将其添加到指令模板中ng-
transclude指令的位置(该指令的模板为transclude时,将对其应用相同的过程)。

因此,这是上面略微更正的自定义指令的注释:

module.directive("tree", function($compile) {
    //Here is the Directive Definition Object being returned 
    //which is one of the two options for creating a custom directive
    //http://docs.angularjs.org/guide/directive
    return {
        restrict: "E",
        //We are stating here the HTML in the element the directive is applied to is going to be given to
        //the template with a ng-transclude directive to be compiled when processing the directive
        transclude: true,
        scope: {family: '='},
        template:       
            '<ul>' + 
                //Here we have one of the ng-transclude directives that will be give the HTML in the 
                //element the directive is applied to
                '<li ng-transclude></li>' +
                '<li ng-repeat="child in family.children">' +
                    //Here is another ng-transclude directive which will be given the same transclude HTML as
                    //above instance
                    //Notice that there is also another directive, 'tree', which is same type of directive this 
                    //template belongs to.  So the directive in the template will handle the ng-transclude 
                    //applied to the div as the transclude for the recursive compile call to the tree 
                    //directive.  The recursion will end when the ng-repeat above has no children to 
                    //walkthrough.  In other words, when we hit a leaf.
                    '<tree family="child"><div ng-transclude></div></tree>' +
                '</li>' +
            '</ul>',
        compile: function(tElement, tAttr, transclude) {
            //We are removing the contents/innerHTML from the element we are going to be applying the 
            //directive to and saving it to adding it below to the $compile call as the template
            var contents = tElement.contents().remove();
            var compiledContents;
            return function(scope, iElement, iAttr) {

                if(!compiledContents) {
                    //Get the link function with the contents frome top level template with 
                    //the transclude
                    compiledContents = $compile(contents, transclude);
                }
                //Call the link function to link the given scope and
                //a Clone Attach Function, http://docs.angularjs.org/api/ng.$compile :
                // "Calling the linking function returns the element of the template. 
                //    It is either the original element passed in, 
                //    or the clone of the element if the cloneAttachFn is provided."
                compiledContents(scope, function(clone, scope) {
                        //Appending the cloned template to the instance element, "iElement", 
                        //on which the directive is to used.
                         iElement.append(clone); 
                });
            };
        }
    };
});

整个工作正常:http//jsfiddle.net/DsvX6/7/

2020-07-04