4

I have ng-repeat with function ng-click inside:

<div ng-repeat="item in data.education_list">
   <a href="" ng-click="deleteEducation(item)">Delete</a>
</div>

I pass object item from ng-repeat to function deleteEducation for deleting element from data.education_list.

So looks function:

$scope.deleteEducation = function (item){
    $scope.data.education_list.splice($scope.data.education_list.indexOf(item), 1);
}

So this way works incorrect sometimes. When I have some element in ng-repeat and after delete item my template HTML is updated and removes row with another item, not that I deleted.

What is right way to delete?

data.education_list is array of objects if do {{data.education_list}}:

[{"name":"Test1","time":"01 Hun 2004 - 12 Sun 2006","Idusereducation":"86","usereducationIdToUser":"702","type":"1"}]

Problem two: If I have object of objects instead array with key:

{"1" : {obj}, 2 : "obj"}

And if I try to delete element from object by key:

delete OBJ[1]; I get the same problem.

3
  • what's in the data.education_list , you forget to catch item in deleteEducation Commented Sep 21, 2015 at 16:35
  • can you provide the data design of data.education_list Commented Sep 21, 2015 at 16:38
  • 1
    provide a demo that replicates this problem Commented Sep 21, 2015 at 17:37

4 Answers 4

3

The easiest way is to use $index, this is a unique identifier that angular adds to track arrays.

<div ng-repeat="item in data.education_list">
   <a href="" ng-click="data.education_list.slice($index,1)">Delete</a>
</div>

If you are filtering a list

you will need to search for the index. Then do the splice. It is a little heavier, but required if you are filtering the list.

JS

this.removeItem = function(item) {
    var index = $scope.data.education_list.indexOf(item);
    if (index != -1) {
        $scope.data.education_list.splice(index, 1);
    }
  };

HTML

ng-click="myctrl.removeItem(item)"

Working Example click to delete and .indexOf vs $index comparison

Sign up to request clarification or add additional context in comments.

8 Comments

This is the correct answer. I stumbled upon this before too, when trying to splice an item in an array inside a forEach loop. xD
If I do track by $index your code will be working?
as soon as you use a filter in ng-repeat this fails. Not sure why you say indexOf doesn't match
If you have duplicates you need to track by $index, but it is not required otherwise
What is this.contacts? is it data.education_list?
|
2
<div ng-repeat="item in data.education_list track by $index">
   <a href="" ng-click="deleteEducation($index)">Delete</a>
</div>

Then

$scope.deleteEducation = function (position){
    $scope.data.education_list.splice(position, 1);
}

Comments

0

Came across the similar problem. In my case, I had to resolve as below just in case if it helps someone.

If you are dealing with objects, please note indexOf works for array not for an Object inside that array. You can do something like below to identify the index and handle this case;

$scope.removeReport = function(report) {
    var index = $scope.contact.reports.map(function(r) { return r.id;}).indexOf(report.id);
        if (index >= 0) {
          $scope.contact.reports.splice(index, 1);
        }
}

1 Comment

With a bit more rep, you will be able to flag duplicate questions like this. Until then, reposting the same answer to multiple questions is not really ideal. Or, if the question is not a duplicate, tailor the answer to this specific question.
0

To remove a child item from a 2 dimensional array in an object, you need to define the parent item and then the child item to splice

e.g. myObject[this.parentIndex].children.splice(this.childIndex,1);

myObject is an Object containing an array that includes children
parentIndex is the Index of the parent items
childIndex is the Index of the child items under the parent.

You can figure out your own way of looping through the parent and child arrays and deciding which child items to remove.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.