AngularJS ngRepeat元素删除

56 javascript angularjs angularjs-directive angularjs-scope

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

但是,我找不到任何有多个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>
Run Code Online (Sandbox Code Playgroud)

为此,我需要创建$ scope.removePhone$ scope.removeEmail,这将使用ngClick on Remove anchor来调用.但我正在寻找更通用的解决方案.特别是因为我有很多页面有很多ngRepeats.

我正在考虑编写一个可以放在Remove锚点上的指令,并且会做类似这样的事情:

  1. 在父元素中查找ngRepeat.
  2. 阅读它的迭代内容(第一种情况为'user.emails',第二种情况为'user.phones')
  3. THAT模型中删除$ index元素.

所以标记看起来像这样:

<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>
Run Code Online (Sandbox Code Playgroud)

我正在寻找可能实现的目标,以及这样做的首选方式是什么?

目前的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);
    };
  });
Run Code Online (Sandbox Code Playgroud)

在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>
Run Code Online (Sandbox Code Playgroud)

Mar*_*man 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);
}
Run Code Online (Sandbox Code Playgroud)

  • 不重复自己是理想的,但保持简单也很重要. (17认同)
  • 有人在使用时注意到延迟吗?好像从对象/数组中删除项目需要几秒钟. (5认同)
  • 您不是在重复自己,而是在不同位置使用不同参数多次调用函数.你需要获得应用程序构建的一天结束,因此应该务实;) (2认同)

Cod*_*rer 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>
Run Code Online (Sandbox Code Playgroud)


小智 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>
Run Code Online (Sandbox Code Playgroud)