AngularJS ngRepeat element removal

后端 未结 5 1246
迷失自我
迷失自我 2021-01-30 08:19

There are quite a few questions on how to implement item removal inside ngRepeat directive, and as I figured out, it comes down to using ngClick and triggering

相关标签:
5条回答
  • 2021-01-30 08:51

    A very simple and convenient way that works cross-browser is to use the 'remove' utility method from the library lodash.

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

    In your controller you declare then

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

    You may of course use indexes if you like. See https://lodash.com/docs#remove

    0 讨论(0)
  • 2021-01-30 09:04

    You could create a generic remove method that would take in the array and the item to remove.

    <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);
    }
    
    0 讨论(0)
  • 2021-01-30 09:04

    If you have used ng-repeat on an object instead of an array, do the following.

    <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];
    }
    

    or the more terse

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

    presumes that the objects look like this

    var emails = {  '123' : { id : '123', .... }  };
    
    var phones = {  '123' : { id : '123', .... }  };
    
    0 讨论(0)
  • 2021-01-30 09:08

    No 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>
    
    0 讨论(0)
  • 2021-01-30 09:17
    <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>
    
    0 讨论(0)
提交回复
热议问题