AngularJS Corner Callback

I often require caching data for a digest cycle, for example, the map / reduce function or deeply nested access. To do this, it would be useful to set a digest callback to clear the cached values ​​before / after the digest cycle. Is there an “angular” way to achieve this?

+4
source share
1 answer

One thing you can do with $rootScope.$watchis to give it a function without a "listener" (call it only one function that returns nothing). This effectively allows you to receive notifications through your provided feature when a digest occurs.

$rootScope.$watch(function() {
    // a digest is happening.
});

During the loop, this listener will be called twice, but I can’t say if I start or end without hacking. So I can just use the flag to help me keep track of the state we are in, and when we move from digestion to a fuzzy digest, we can clear our cache. In this example, I just create a service that creates a cache with $cacheFactoryand returns it, but monitors this state and clears it at the right time.

. , - . , watch . , , , , .

, $rootScope, $$postDigest, , , . , , $$postDigest, . , , API. .

+8

Source: https://habr.com/ru/post/1539901/


All Articles