Angular 指令中的递归

如何解决Angular 指令中的递归

受@dnc253 提到的线程中描述的解决方案的启发,我将递归功能抽象为一个服务

module.factory('RecursionHelper', ['$compile', function($compile){
    return {
        /**
         * Manually compiles the element, fixing the recursion loop.
         * @param element
         * @param [link] A post-link function, or an object with function(s) registered via pre and post properties.
         * @returns An object containing the linking functions.
         */
        compile: function(element, link){
            // Normalize the link parameter
            if(angular.isFunction(link)){
                link = { post: link };
            }

            // Break the recursion loop by removing the contents
            var contents = element.contents().remove();
            var compiledContents;
            return {
                pre: (link && link.pre) ? link.pre : null,
                /**
                 * Compiles and re-adds the contents
                 */
                post: function(scope, element){
                    // Compile the contents
                    if(!compiledContents){
                        compiledContents = $compile(contents);
                    }
                    // Re-add the compiled contents to the element
                    compiledContents(scope, function(clone){
                        element.append(clone);
                    });

                    // Call the post-linking function, if any
                    if(link && link.post){
                        link.post.apply(null, arguments);
                    }
                }
            };
        }
    };
}]);

使用如下:

module.directive("tree", ["RecursionHelper", function(RecursionHelper) {
    return {
        restrict: "E",
        scope: {family: '='},
        template: 
            '<p>{{ family.name }}</p>'+
            '<ul>' + 
                '<li ng-repeat="child in family.children">' + 
                    '<tree family="child"></tree>' +
                '</li>' +
            '</ul>',
        compile: function(element) {
            // Use the compile function from the RecursionHelper,
            // And return the linking function(s) which it returns
            return RecursionHelper.compile(element);
        }
    };
}]);

请参阅此Plunker进行演示。我最喜欢这个解决方案,因为:

  1. 您不需要使您的 html 不那么干净的特殊指令。
  2. 递归逻辑被抽象到 RecursionHelper 服务中,因此您可以保持指令干净。

更新:从 Angular 1.5.x 开始,不再需要任何技巧,但仅适用于 template ,不适用于 templateUrl

解决方法

有几个流行的递归角度指令问答,它们都归结为以下解决方案之一:

  • 根据运行时范围状态手动增量“编译”HTML
  • 根本不使用指令,而是使用引用自身的