删除选中的行Angular

时间:2015-10-21 13:40:00

标签: angularjs checkbox row

我有一个字段列表。用户可以添加行。如何创建一个删除已检查行的函数?我想在新数组中添加它们,但我不知道如何过滤所选项目。

http://plnkr.co/edit/S2tudP0kJcVUiKZKRa7j?p=preview

  <body ng-controller="DuplicateInputCtrl" class="container">
<div data-ng-repeat="food in foods">
  <div class="form-group title-field">
    <label for="">Food</label>
    <select class="form-control input-full" data-ng-model="food.Selection"
        data-ng-options="foodType.code as foodType.type for foodType in foodTypes">
        <option value="">Select</option>
    </select>
    <input type="hidden">
    <button data-ng-click="removeItem($index)" class="btn delete-field-{{$index}}">
      Delete
    </button>
  </div>
  <div class="form-group">

        <input style="background: white; color: black;" type="text" id="myInput"  class="form-control" data-ng-model="food.Text"/>

    </div>
</div>
{{foods | json}}
<button data-ng-click="cloneItem()" class="btn inline">
  Add
</button>    
    <button data-ng-click="" class="btn inline">
  Remove Selected
</button>  

谢谢!

2 个答案:

答案 0 :(得分:3)

你可以做这样的事情

 $scope.removeSelected = function() {
            $scope.foods = $scope.foods.filter(function(food){
                return !food.selected
            })
          }

这是example

答案 1 :(得分:1)

Use the following function on remove selected button click 
`$scope.removeSelectedItems = function () {
          for (var i = 0; i < $scope.foods.length; i++) {                 
              if ($scope.foods[i].selected == true) {
                  $scope.foods.splice(i, 1);
                  i--; 
              }
          }             
      }`