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.
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.
Share Improve this question asked Mar 4, 2014 at 7:30 xmarstonxmarston 8833 gold badges13 silver badges38 bronze badges 5- Can you show the code that does the deletion and the html fragment containing the ng-repeat? – Gruff Bunny Commented Mar 4, 2014 at 7:50
- @refrigerator No, it is an example only. I have an array of differents images(src). – xmarston Commented Mar 4, 2014 at 8:39
- @GruffBunny here you have the code: Plunker – xmarston 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) – Gruff Bunny Commented Mar 4, 2014 at 9:15
- @GruffBunny Images is an array of objects. – xmarston Commented Mar 4, 2014 at 9:19
3 Answers
Reset to default 3The 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.
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
})
long answer: https://github./angular/angular.js/wiki/Understanding-Scopes
short answer, switch your array to be:
[{label:'stuff'},...]