Is there a way to disable the launch of $ digest on every $ http.get?

Background

angular $http service runs $digest on each get (if no $digest already running):

 if (!$rootScope.$$phase) $rootScope.$apply(); 

In addition to extracting objects from our API, our application has many directives with templateUrl - which are extracted angular using $http . This results in hundreds of $digest cycles on a cold start.

Commenting on the above line, the number of $digest cycles will decrease to about 3, and the application will be faster than MUCH , without any bindings (at least because of $http $digest does not start).

Question

Is there a way to disable $ http starting $ digest?

+8
javascript angularjs digest
source share
2 answers

use $httpProvider.useApplyAsync(true); in the application configuration. then the template directive, loaded within 10 ms, will encounter the same digest. this will reduce the digest cycle cycle. see here

+3
source

It may not be possible to do what you want directly, but you can limit the impact of these digest cycles. Try placing <div ng-if="everythingLoaded"> on most of the page and set everythingLoaded to true after all your $http calls have completed.

For example:

 var p1 = $http.get(...); var p2 = $http.get(...); Promises.all([p1, p2]).then(function() { $scope.everythingLoaded = true; }); 

The digest loop will still work, but it will be much faster, because ng-if will eliminate the effect on the DOM and most of your controller until the condition is true.

I am sure that the use of template caching will be solved for the problem of loading the template, assuming that this is still a problem in recent versions of angular.

0
source

All Articles