Angularjs 带函数的角度递归指令
我是个新来的,我想把我的头绕在这些望远镜上。我希望这些函数可以从我的树中继承下来。下面的代码是我所拥有的,它适用于第一级,但在那之后,我有点迷路了。有什么想法吗Angularjs 带函数的角度递归指令,angularjs,Angularjs,我是个新来的,我想把我的头绕在这些望远镜上。我希望这些函数可以从我的树中继承下来。下面的代码是我所拥有的,它适用于第一级,但在那之后,我有点迷路了。有什么想法吗 <tree get-branch="GetBranch(parent, $event)" show-menu="ShowMenu(parent)" family="i"></tree> $scope.GetBranch = function (parent, $event) {... $scope.ShowM
<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.
});
}
};
});
如果您希望某个函数在任何地方都可用,或者任何值在应用程序中的任何地方共享,我认为您的方法是错误的。这通常是应该在服务或工厂中进行的。查看快速搜索:我是否应该使用服务,然后使用链接将其添加到我的指令范围?或者我怎么把它放进去?是的,你应该像使用$scope或任何角度服务一样,通过注射来获得它。例如,如果您的服务名为“myFunctions”,那么只需在$scope之外添加myFunctions即可