嵌套在ng-repeat中的拼接数组,

时间:2016-04-26 14:59:18

标签: javascript angularjs repeat

数组结构如此

  $scope.structure = {
    sections: [{
      id:0,
      sectionItems: []
    },{
      id:1,
      sectionItems: []
    },{
      id:2,
      sectionItems: []
    }]
};

我有一个嵌套的ng-repeat,所以我可以在sectionItems []中显示项目 (里面应该是对象,其中一个键是名称 - 不相关)

<div ng-repeat="sections in structure.sections" class="col-md-12">
   <div class="panel panel-info">
    <ul class="screenW-section">
        <li class="col-xs-6" ng-repeat="item in sections.sectionItems" 
         ng-click="item.splice($index, 1)">
            {{item.Name}}
        </li>
    </ul>
</div> </div>

我希望能够删除点击的项目,但是 ng-click =“item.splice($ index,1) 无论我如何格式化它都不起作用。

3 个答案:

答案 0 :(得分:3)

要删除项目,您需要将其从阵列中删除 所以,例如,你可以做到

ng-click="remove(sections.sectionItems, $index)"

在视图中,

$scope.remove = function(array, index) { 
  array.splice(index, 1);     
}
控制器中的

......

答案 1 :(得分:3)

试试这个:

&#13;
&#13;
var app = angular.module("testApp", []);
app.controller('testCtrl', function($scope){
  $scope.structure = {
    sections: [{
      id:0,
      sectionItems: ['1','2','3']
    },{
      id:1,
      sectionItems: ['11','21','32']
    },{
      id:2,
      sectionItems: ['12','22','32']
    }]
};
  
  
  $scope.remove = function(sectionItems,index){
     sectionItems.splice(index, 1);     
    }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="testApp" ng-controller="testCtrl">

  <div ng-repeat="sections in structure.sections" class="col-md-12">
   <div class="panel panel-info">
    <ul class="screenW-section">
        <li class="col-xs-6" ng-repeat="item in sections.sectionItems" 
         ng-click="remove(sections.sectionItems,$index)">
            {{item}}
        </li>
    </ul>
</div> </div>

</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

你在项目而不是数组上调用splice

ng-click="sections.sectionItems.splice($index, 1)"