最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS ng-repeat not updating properly when delete items in array - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 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.

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'},...]
发布评论

评论列表(0)

  1. 暂无评论