角度-拼接功能始终删除最后一个元素 [英] Angular - splice function always removes last element

查看:42
本文介绍了角度-拼接功能始终删除最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象数组,当单击Delete键时,我想删除某些对象.但是,无论我创建了多少行,它始终会从rows数组中删除最后一项.即使我显式地放入$ scope.rows.splice(1,1)这样的行,它仍然会删除最后一个元素,而不是第二个.

I have an array of objects that I want to remove certain objects when a Delete key is clicked. However, it always removes the last item from the rows array, no matter how many rows I have created. 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.

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) {

      }
    }
  });

HTML的相关代码段

The relevant snippet of HTML

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

柱塞: http://plnkr.co/edit/rDkXpIgiOSoNvLNPsVbP

测试:单击添加行3次.然后单击第二行上的删除.您会看到它实际上删除了第三行,而不是第二行

Test: Click on Add Row 3 times. Then click on Delete on second row. You will see that it actually removes the 3 row, not 2nd

推荐答案

请明确@ZsoltGyöngyösi给出的答案:

Just to be clear with the answer @ZsoltGyöngyösi gave:

每个具有 id 字段的元素都需要 ng-model ="$ parent.row.field"

因此,如果您通过以下方式设置 queryRowTemplate.hml ,则会删除正确的行:

Thus, the correct row will be deleted if you setup queryRowTemplate.hml this way:

<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 />

在此处查看Plunkr

这篇关于角度-拼接功能始终删除最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆