AngularJS ng重复元素删除

时间:2013-08-15 11:32:12

标签: javascript angularjs angularjs-directive angularjs-scope

关于如何在ngRepeat指令中实现项目删除有很多问题,正如我所知,它归结为使用 ngClick 并触发一些删除函数传递项目的 $索引

但是,我找不到任何有多个ngRepeats的示例:

<div ng-controller="MyController">
    <div ng-repeat="email in user.emails">
        {{ email }} <a href>Remove</a>
    </div>

    <div ng-repeat="phone in user.phones">
        {{ phone }} <a href>Remove</a>
    </div>
</div>

为此,我需要创建 $ scope.removePhone $ scope.removeEmail ,这些将在上使用 ngClick 进行调用>删除锚。但我正在寻找更通用的解决方案。特别是因为我有很多页面许多ngRepeats

我正在考虑编写一个可以放在删除锚点上的指令,并且会执行以下操作:

  1. 在父元素中查找 ngRepeat
  2. 阅读它的迭代内容(第一种情况为'user.emails',第二种情况为'user.ephones')
  3. THAT 模型中删除 $ index 元素。
  4. 所以标记看起来像这样:

    <div ng-controller="MyController">
        <div ng-repeat="email in user.emails">
            {{ email }} <a href remove-directive="$index">Remove</a>
        </div>
    
        <div ng-repeat="phone in user.phones">
            {{ phone }} <a href remove-directive="$index">Remove</a>
        </div>
    </div>
    

    我正在寻找可能实现的目标以及最佳方法吗?

    目前的hacky解决方案

    以下是我目前的工作方式。这很丑陋而且很丑陋,但直到我找到一个更漂亮的方式才能完成工作。

      myAppModule.controller('MyController', function ($scope, $parse, $routeParams, User) {
        $scope.user = User.get({id: $routeParams.id});
    
        $scope.remove = function ($index, $event) {
          // TODO: Find a way to make a directive that does this. This is ugly. And probably very wrong.
          var repeatExpr = $($event.currentTarget).closest('[ng-repeat]').attr('ng-repeat');
          var modelPath  = $parse(repeatExpr.split('in')[1].replace(/^\s+|\s+$/g, ''));
    
          $scope.$eval(modelPath).splice($index, 1);
        };
      });
    

    在DOM中:

    <div ng-repeat="email in user.email" class="control-group">
      <label class="control-label">
        {{ "Email Address"|_trans }}
      </label>
    
      <div class="controls">
        <input type="text" ng-model="email.address">
    
        <span class="help-inline"><a href ng-click="remove($index, $event)">{{ "Delete"|_trans }}</a></span>
      </div>
    </div>
    

5 个答案:

答案 0 :(得分:78)

您可以创建一个通用的删除方法,该方法将接收数组和要删除的项目。

<div ng-app="" ng-controller="MyController">
    <div ng-repeat="email in emails">{{ email }} <a ng-click="remove(emails, $index)">Remove</a>
    </div>
    <div ng-repeat="phone in phones">{{ phone }} <a ng-click="remove(phones, $index)">Remove</a>
    </div>
</div>

$scope.remove = function(array, index){
    array.splice(index, 1);
}

答案 1 :(得分:71)

没有JS

<div ng-repeat="option in options" ng-init=options=[1,2,3,4,5]>
   <button ng-click="options.splice($index,1)">Remove me</button>      
</div>

答案 2 :(得分:19)

<div ng-app="" ng-controller="MyController">
    <div ng-repeat="email in emails as datasource">{{ email }} 
        <a ng-click="datasource.splice($index,1)">Remove</a>
    </div>
    <div ng-repeat="phone in phones as datasource">{{ phone }} 
        <a ng-click="datasource.splice($index,1)">Remove</a>
    </div>
</div>

答案 3 :(得分:3)

跨浏览器工作的一种非常简单方便的方法是使用库lodash中的'remove'实用程序方法。

<div ng-repeat="phone in phones">{{ phone }} 
  <a ng-click="removeItem(phones, phone)">Remove</a>
</div>

在您的控制器中,您声明

//inject lodash dependency

//declare method in scope
$scope.removeItem = function(list, item){
   lodash.remove(list,function(someItem) { return item === someItem});
}

如果您愿意,您当然可以使用索引。见https://lodash.com/docs#remove

答案 4 :(得分:1)

如果您在对象上使用ng-repeat而不是数组,请执行以下操作。

<div ng-app="" ng-controller="MyController">
    <div ng-repeat="email in emails">{{ email }} 
      <a ng-click="remove(emails, email)">Remove</a>
    </div>
    <div ng-repeat="phone in phones">{{ phone }} 
      <a ng-click="remove(phones, phone)">Remove</a>
    </div>
</div>

$scope.remove = function(objects, o){
    delete object[o.id];
}

或更简洁

<div ng-app="" ng-controller="MyController">
    <div ng-repeat="email in emails">{{ email }} 
      <a ng-click="delete emails[email.id]">Remove</a>
    </div>
    <div ng-repeat="phone in phones">{{ phone }} 
      <a ng-click="delete phones[phone.id]">Remove</a>
    </div>
</div>

假设对象看起来像这样

var emails = {  '123' : { id : '123', .... }  };

var phones = {  '123' : { id : '123', .... }  };