简体   繁体   English

Angular - splice函数总是删除最后一个元素

[英]Angular - splice function always removes last element

I have an array of objects that I want to remove certain objects when a Delete key is clicked. 我有一个对象数组,我想在单击Delete键时删除某些对象。 However, it always removes the last item from the rows array, no matter how many rows I have created. 但是,无论我创建了多少行,它总是从rows数组中删除最后一项。 Even if I explicitly put in a line like so, $scope.rows.splice(1,1) - it will still remove the last element, not the second. 即使我明确地输入了这样的行,$ scope.rows.splice(1,1) - 它仍将删除最后一个元素,而不是第二个元素。

JS JS

angular.module('app', ['ngAnimate', 'ui.bootstrap'])
  .directive('queryBuilder', function() {
    return {
      restrict: 'E',
      scope: {},
      controller: function($scope) {
        $scope.rows = [{}]

        $scope.$on('addRowRqst', function(evt) {
          // evt.stopPropagation()
          $scope.rows.push({})
        });
        $scope.$on('removeRowRqst', function(evt, args) {
          // evt.stopPropagation()
          //THIS IS WHERE THE REMOVE HAPPENS 
          $scope.rows.splice($scope.rows.indexOf(args),1);
        });        
      },
      templateUrl: 'queryBuilderTemplate.html',
    }
  }).directive('queryRow', function() {
    return {
      scope: {},
      restrict: 'EA',
      templateUrl: 'queryRowTemplate.html',
      controller: function($scope) {
        $scope.addRqst = function() {
          $scope.$emit('addRowRqst')
        };
        $scope.removeRqst = function(index) {
          $scope.$emit('removeRowRqst', index)
        };
      },
      link: function(scope, elem, attrs) {

      }
    }
  });

The relevant snippet of HTML HTML的相关片段

....
<button class="btn btn-default" ng-click="removeRqst($parent.row)" type="submit">Delete Row</button>
....

Plunker: http://plnkr.co/edit/rDkXpIgiOSoNvLNPsVbP Plunker: http ://plnkr.co/edit/rDkXpIgiOSoNvLNPsVbP

Test: Click on Add Row 3 times. 测试:单击添加行3次。 Then click on Delete on second row. 然后单击第二行上的“删除”。 You will see that it actually removes the 3 row, not 2nd 你会看到它实际上删除了3行,而不是第2行

Just to be clear with the answer @ZsoltGyöngyösi gave: 为了清楚答案@ZsoltGyöngyösi给出了:

Every element with and id field needs ng-model="$parent.row.field" 带有和id字段的每个元素都需要ng-model="$parent.row.field"

Thus, the correct row will be deleted if you setup queryRowTemplate.hml this way: 因此,如果queryRowTemplate.hml这种方式设置queryRowTemplate.hml将删除正确的行:

queryRowTemplate.html queryRowTemplate.html

<div class="form-group col-md-3">
  <label for="selectedField">Select Field</label>
  <select id="selectedField" class="form-control" ng-model="$parent.row.field">
    <option>title</option>
    <option>application</option>
    <option>subject</option>
    <option>filetype</option>
  </select>
</div>
<div class="form-group col-md-3">
  <label for="logicalOperator">Logical Operator</label>
  <select id="logicalOperator" class="form-control" ng-model="$parent.row.logical">
    <option>equal to</option>
    <option>not equal to</option>
  </select>
</div>
<div class="form-group col-md-3">
  <label for="searchText">Search</label>
  <input id="searchText" class="form-control" type="text" placeholder="search..." ng-model="$parent.row.search" />
</div>
<div class="form-group col-md-3">
  <label for="operator">Operator (optional)</label>
  <select id="operator" class="form-control" ng-model="$parent.row.operator">
    <option value=""></option>
    <option>AND</option>
    <option>OR</option>
  </select>
</div>
<button class="btn btn-default" ng-click="addRqst()" type="submit">Add Row</button>
<button class="btn btn-default" ng-click="removeRqst($parent.row)" type="submit">Delete Row</button>
{{$parent.$index}}
<hr />

See Plunkr, here 请参见Plunkr

The code is fine, the correct form is being deleted. 代码很好,正在删除正确的表单。 The problem is that you are not binding your view to the queryRow directive, so it seems that the last one is being deleted. 问题是你没有将你的视图绑定到queryRow指令,所以似乎最后一个被删除。 In reality, angular rebuilds your view based on the array, not knowing about the content of the templates. 实际上,angular会根据数组重建您的视图,而不了解模板的内容。 Hence unbound input fields simply retain the values, except for the last one. 因此,未绑定的输入字段只保留值,但最后一个除外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM