How to remove an item from an array in AngularJS scope?

后端 未结 10 1693
梦毁少年i
梦毁少年i 2020-11-27 09:10

Simple to-do list, but with a delete button on list page for each item:

Relevant template HTML:


          


        
相关标签:
10条回答
  • 2020-11-27 09:48

    To remove a element from scope use:

    // remove an item
        $scope.remove = function(index) {
            $scope.items.splice(index, 1);
        };
    

    From enter link description here

    0 讨论(0)
  • 2020-11-27 09:51

    Angular have a built-in function called arrayRemove, in your case the method can simply be:

    arrayRemove($scope.persons, person)
    
    0 讨论(0)
  • 2020-11-27 09:55

    For the the accepted answer of @Joseph Silber is not working, because indexOf returns -1. This is probably because Angular adds an hashkey, which is different for my $scope.items[0] and my item. I tried to resolve this with the angular.toJson() function, but it did not work :(

    Ah, I found out the reason... I use a chunk method to create two columns in my table by watching my $scope.items. Sorry!

    0 讨论(0)
  • 2020-11-27 09:55
    array.splice(array.pop(item));
    
    0 讨论(0)
提交回复
热议问题