Leon Gaban Leon Gaban - 1 year ago 62
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?

Answer Source

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

feedList.splice(0, 0, item)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download