当前位置: 首页 > 面试题库 >

AngularJS:了解递归指令

濮阳唯
2023-03-14
问题内容

我在这里找到了一个很棒的树指令。原文: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); 
                });
            };
        },

问题答案:

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/



 类似资料:
  • 问题内容: 我在上面直接写了上面的内容,因此可能无法编译,但认为可以。 任何人都可以从存储的角度来简短地解释它的工作原理吗?它通过计算5 (5-1)开始,然后依次下降到4 (4-1)然后是3 *(3-1).....直到达到1,它将只返回1,对吗?抱歉,我太粗略了,我只想知道这是如何工作的 谢谢 但随着工作的进行,它将获得各个阶段的值 5 (5-1)4 (4-1)… … … 这些如何存储然后取回,或

  • 问题内容: 我想扩展一些递归的属性(又名深度复制)。就像jQuery一样。我不只包括jquery一件事的b / c。 您知道有什么优雅的方法可以使用简单的javascript或angularjs吗? 更新, 请看看并尝试完成相同的结果 http://plnkr.co/edit/GHabYbyhsqtfBPtplksO?p=preview 我确实调查了.copy()但“属性(对象)已删除” 问题答案

  • 问题内容: 在这个sqlfiddle中… http://sqlfiddle.com/#!6/b6587/6 我收到以下错误…。 声明终止。在语句完成之前,最大递归100已用尽。 我知道CTE第二选择的where子句中需要进行“终止检查”。即使您取消注释WHERE子句,我也会遇到相同的错误。 我只是想了解1)为什么根本需要它……毕竟每个订单行都与每个客户行都有关系,2)由于需要“终止检查”,因此该示

  • 编写一个方法writeChars,该方法接受整数参数n,并按如下方式输出n个字符。输出的中间字符应始终为星号(“*”)。如果要求您写出偶数个字符,则中间会有两个星号(“**”)。在星号之前,您应写出少于个字符(“ 我已经设法解决了这个问题,但不太明白一句话: 为什么是递归情况: n-2?而不是n-1?

  • 对于,我被赋予和。 现在给定N、A、B、C和X,如何有效地找到所有N个元素? 我需要把这N个元素分成2组,其中最大的元素在第一组,第二大的元素在第二组,第三大的元素在第一组,以此类推。。。最后需要找到两个集合元素之和的绝对差。 我可以在不计算所有元素的情况下找到这个差异吗?因为N可以大到最大值为100。

  • 正如标题所解释的,我有一个非常基本的编程问题,但我还没有找到。过滤掉所有(非常聪明的)“为了理解递归,你必须首先理解递归。”各种在线线程的回复我仍然不太明白。 当我们面对不知道自己不知道的事情时,我们可能会提出错误的问题或错误地提出正确的问题。我将分享我的“想法”。我的问题是希望有类似观点的人能够分享一些知识,帮助我打开递归灯泡! 以下是函数(语法用Swift编写): 我们将使用2和5作为参数: