带函数的Angular递归指令

时间:2014-12-16 10:44:02

标签: angularjs

角色新手,试图绕着这些范围包围我。我希望函数可以在我的树下继承。下面的代码是我所拥有的,它适用于第一级,但在此之后,我有点迷失。有什么想法吗?

<tree get-branch="GetBranch(parent, $event)" show-menu="ShowMenu(parent)" family="i"></tree> 

$scope.GetBranch = function (parent, $event) {...
$scope.ShowMenu = function(parent) {...

App.directive("tree", function (RecursionHelper) {
return {
    restrict: "E",
    scope: {
        getBranch: "&",
        showMenu: "&",
        family: '='
    },
    template:
        '<a ng-right-click="showMenu({ parent: family })" ng-click="getBranch({parent:family, $event:$event})" >{{ family.Alias }}</a>' +
        '<ul ng-if="family.IsCategory">' +
            '<li ng-repeat="child in family.Children" ng-class="{category: child.IsCategory}">' +
                '<tree get-branch="getBranch({family:family, $event:$event})" show-menu="showMenu({ family: family })" family="child"></tree>' +
            '</li>' +
        '</ul>',
    compile: function (element) {
        return RecursionHelper.compile(element, function (scope, iElement, iAttrs, controller, transcludeFn) {
            // Define your normal link function here.
            // Alternative: instead of passing a function,
            // you can also pass an object with 
            // a 'pre'- and 'post'-link function.
        });
    }
};
});

1 个答案:

答案 0 :(得分:0)

如果您希望函数可以在任何地方使用,或者在应用程序的任何位置共享任何值,我认为您的方法是错误的。这通常应该在服务或工厂中。看看https://docs.angularjs.org/guide/services