4

The problem that I'm facing is that I have a ng-repeat and when I delete an item by clicking a button with a function associated to delete items in array the ng-repeat not shows properly the actual array.

The array looks like:

['stuff', 'stuff', 'stuff', ....]

What shows ng-repeat when I delete an item is the array without the last position although I deleted the first position. When I perform a console.log the array looks correct, the first position or x position was removed.

5
  • Can you show the code that does the deletion and the html fragment containing the ng-repeat? Commented Mar 4, 2014 at 7:50
  • @refrigerator No, it is an example only. I have an array of differents images(src). Commented Mar 4, 2014 at 8:39
  • @GruffBunny here you have the code: Plunker Commented Mar 4, 2014 at 8:46
  • Is Images an array or an object? The reason I ask is that the ng-repeat is treating it as an object and when you delete you treat it as an array (using splice) Commented Mar 4, 2014 at 9:15
  • @GruffBunny Images is an array of objects. Commented Mar 4, 2014 at 9:19

3 Answers 3

3

The problem was the:

track by $index

Due to some duplicate images in the proofs I've been doing. I removed it and it works as expected.

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

1 Comment

This worked for me as well. I used it because angular was complaining about duplicates. To stop angular complaining about duplicates I used aron.duby's answer: instead of ['a', 'a','a'] I used [{'text':'a'}, {'text':'a'}] and got no more duplicates errors.
2

I was having this issue, I eventually narrowed it down to having something to do with the angular not being notified that something changed. To work around this, try using $apply. So something like this:

$rootScope.$apply(function() {
  // remove the item from the array 
})

Comments

1

long answer: https://github.com/angular/angular.js/wiki/Understanding-Scopes

short answer, switch your array to be:

[{label:'stuff'},...]

1 Comment

My array looks right now [Object { label="xxx.jpg"}, Object { label="xxx.jpg"}, Object { label="xxx.jpg"}, Object { label="xxx.jpg"}]but I'm getting the same results...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.