Leon Gaban Leon Gaban - 3 months ago 11x
AngularJS Question

How to add item into array and make it appear at the top, without triggering multiple digest cycles?


vs.unshift = function() {

vs.push = function() {

In our real app with APIs, when I use
to add a new alert into the alerts feed (via websockets) the item takes forever to get added to the top of the array because of the digest cycle. It seems to iterate over every time in the array by the amount of items in the array (ie: 10x10), before it finally adds the item to the top of the array.

However if we use
the new alert is immediately added because Angular does not have to check all the other items in the array first.

This problem is hard to replicate in the plnkr example I created above, because the unshift instantly adds the item to the top of the array. However, how can one use
and then move the item that was pushed to the top of the array without causing a digest cycle?


As @Redu suggested, the splice method works without causing the Angular digest cycle to hang.

feedList.splice(0, 0, item)