简体   繁体   English

如何在angularjs中的递归指令中使用全局函数

[英]How to use a global function in a recursive directive in angularjs

I'm trying to use a global function defined in a parent constructor within the directives on the page which will be called recursively. 我正在尝试使用在页面上的指令中的父构造函数中定义的全局函数,该指令将被递归调用。 However not able to figure out how to pass it through all the layers cleanly. 然而,无法弄清楚如何干净地通过所有层。 using the $parent I can pass it through one layer only.. What would be the best way to achieve this? 使用$ parent我只能通过一层传递它。实现这一目标的最佳方法是什么?

http://jsfiddle.net/J8tFS/ http://jsfiddle.net/J8tFS/

<div ng-app="myapp">
<div ng-controller="TreeCtrl">
    <tree family="treeFamily"></tree>
</div>

module.controller("TreeCtrl", function($scope) {
$scope.treeFamily = {
    name : "Parent",
    children: [{
        name : "Child1",
        children: [{
            name : "Grandchild1",
            children: []
        },{
            name : "Grandchild2",
            children: []
        },{
            name : "Grandchild3",
            children: []
        }]
    }, {
        name: "Child2",
        children: []
    }]
};
$scope.toUpper = function(strInput) {
     return strInput.toUpperCase();   
}
});

module.directive("tree", function($compile) {
return {
    restrict: "E",
    scope: {family: '='},
    template: 
        '<p>{{ family.name }}</p>'+
        '<p>{{ $parent.toUpper(family.name) }}</p>' +
        '<ul>' + 
            '<li ng-repeat="child in family.children">' + 
                '<tree family="child"></tree>' +
            '</li>' +
        '</ul>',
    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); 
            });
        };
    }
};

}); });

Do you realy need to use isolated scope? 你真的需要使用隔离范围吗? If no, just do not use it and get access to everything in parent scopes. 如果不是,请不要使用它并访问父作用域中的所有内容。

module.directive("tree", function($compile) {
return {
    restrict: "E",
    template: 
        '<p>{{ family.name }}</p>'+
        '<p>{{ $parent.toUpper($parent.family.name) }}</p>' +
        '<ul>' + 
            '<li ng-repeat="child in family.children">' + 
                '<tree family="child"></tree>' +
            '</li>' +
        '</ul>',
    compile: function(tElement, tAttr) {
        var contents = tElement.contents().remove();
        var compiledContents;
        return function(scope, iElement, iAttr) {
            scope.family = scope.$eval(iAttr.family);

            if(!compiledContents) {
                compiledContents = $compile(contents);
            }
            compiledContents(scope, function(clone, scope) {
                     iElement.append(clone); 
            });
        };
    }
};
});

Here is fixed fiddle: http://jsfiddle.net/J8tFS/1/ 这是固定的小提琴: http//jsfiddle.net/J8tFS/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM