2013-09-21 44 views

回答

31

看到一個工作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> 
+0

嗨Bekos,這是我正在尋找。你解決了我的問題。謝謝你 – user2801604

+0

非常感謝!我不知道$ event var,所以這非常有幫助! –

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); 
    };