puopg
puopg

Reputation: 573

$scope.$evalAsync vs $scope.$applyAsync

What is the difference between $evalAsync and $applyAsync? My understanding is that when I use $evalAsync from a directive, the expression will evaluate before the browser renders.

So as an example, if I wanted to scroll to a particular position on a page but not show the jump to that spot, I could use this to scroll to the position and since it fires before the browser has rendered, this visual bug would be gone.

However, what is the purpose of applyAsync? When is it a good idea to use one over the other?

Upvotes: 11

Views: 8273

Answers (2)

Luckylooke
Luckylooke

Reputation: 4539

The

$evalAsync()

will execute in the current digest

$applyAsync()

in a scheduled one.

If you need details: Ben Nadel or stack here

Upvotes: 10

puopg
puopg

Reputation: 573

Here is what I have been using $applyAsync for. Basically, I use this as a safe $apply. You know that annoying error when you try to trigger a digest when one is already in progress? If you use $applyAsync, you will get another digest, but it will occur when the current digest cycle has completed.

$applyAsync is also cool since you can batch up a bunch of callbacks to fire within then next digest.

Upvotes: 2

Related Questions