我有以下代码。它遍历 JSON 以生成嵌套的 ul 列表。我有一个单击事件,它执行绑定到锚标记的函数 toggleNav()。我不知道为什么单击事件会两次绑定到元素。我也是 Angular 的新手,有没有解释这个概念的文件?谢谢!
define([
'/assets/angularapp/AppDirectives.js',
'highstock'
], function (directives) {
directives.directive('collection', function () {
return {
restrict: "E", //declare by element
replace: true,
scope: {
collection: '='
},
template: "<ul class=\"nav nav-list tree\"><member ng-repeat=\"member in collection\" member=\"member\"></member></ul>"
}
})
directives.directive('member', function ($compile) {
return {
restrict: "E",
replace: true,
scope: {
member: '='
},
template: "<li ng-show=\"member.open\"><span><input type=\"checkbox\" ng-model=\"member.selected\" class=\"sideChkbox\"><a class=\"tree-toggle\" ng-click=\"toggleNav()\"><i class=\"icon-chevron-right\"></i>{{member.data}}</a></span></li>",
controller: function($scope, $element){
$scope.toggleNav = function(){
angular.forEach($scope.member.children,function(child,key){
if(child.open==true){
alert("a")
child.open=false;
} else {
child.open=true;
alert("b")
}
})
}
},
link: function (scope, element, attrs) {
if (angular.isArray(scope.member.children)) {
element.append("<collection collection='member.children'></collection>");
$compile(element.contents())(scope)
}
}
}
})