栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

AngularJS:了解递归指令

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

AngularJS:了解递归指令

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/



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/394614.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号