我在我的模板中使用手风琴指令,在手风琴内部我正在添加attr
ng-click
一个元素。当我单击该元素时,它会触发两次。
小提琴:http: //jsfiddle.net/Lvc0u55v/10071/
代码 :
//accordion directive
.directive('simpleAccordion', function($compile, $timeout) {
return {
restrict: 'AEC',
controller: function($scope) {
$scope.current = null;
$scope.height = [];
$scope.zero = {
height: 0
};
$scope.toggle = function(i) {
$scope.current = $scope.current === i ? null : i;
};
},
link: function(scope, el, attrs) {
var itemSelector = '[item-selector]' || 'li',
titleSelector = '[title-selector]' || 'h2',
contentSelector = '[content-selector]' || 'div';
$timeout(function(){
var items = el[0].querySelectorAll(itemSelector);
for (var i in items) {
if (angular.isObject(items[i])) {
var title = items[i].querySelectorAll(titleSelector)[0];
var content = items[i].querySelectorAll(contentSelector)[0];
scope.height.push({
'height': (content.offsetHeight + 10) + 'px'
});
angular.element(items[i]).addClass('item').attr({
'ng-class': '{\'open\':current == ' + i + '}'
});
angular.element(title).addClass('title').attr('ng-click', 'toggle(' + i + ')');
angular.element(content).addClass('content').attr({
'ng-style': 'current == ' + i + '?height[' + i + ']:zero'
});;
}
}
$compile(angular.element(el).contents())(scope);
});
}
}
})
小提琴:http: //jsfiddle.net/Lvc0u55v/10071/