我正在使用来自http://angular-ui.github.com/bootstrap/的手风琴指令,我需要在标题部分有两个按钮。
我是AngularJS的新手,请帮助我实现这一目标。
答案 0 :(得分:33)
查看有效的plunker。
您只需在控制器中添加和删除功能
$scope.addGroup = function(idx, group, e) {
if (e) {
e.preventDefault();
e.stopPropagation();
}
var newGroup = angular.copy(group);
newGroup.no = $scope.groups.length + 1;
$scope.groups.splice(idx + 1, 0, newGroup);
};
$scope.removeGroup = function(idx, e) {
if (e) {
e.preventDefault();
e.stopPropagation();
}
$scope.groups.splice(idx, 1);
};
和你的html的ng-repeat
:
<accordion close-others="oneAtATime">
<accordion-group heading="{{group.title}}" ng-repeat="group in groups">
<accordion-heading>
{{ group.title }} ({{group.no}})
<button class="btn btn-small" ng-click="addGroup($index, group, $event)">+</button>
<button class="btn btn-small" ng-click="removeGroup($index, $event)" ng-show="$index">-</button>
</accordion-heading>
{{group.content}}
</accordion-group>
</accordion>
答案 1 :(得分:1)
把这个e.originalEvent.cancelBubble = true;
$scope.addGroup = function(idx, group, e) {
if (e) {
e.originalEvent.cancelBubble=true;
}
var newGroup = angular.copy(group);
newGroup.no = $scope.groups.length + 1;
$scope.groups.splice(idx + 1, 0, newGroup);
};
$scope.removeGroup = function(idx, e) {
if (e) {
e.originalEvent.cancelBubble=true;
}
$scope.groups.splice(idx, 1);
};