Background
angular's $http
service is triggering a $digest
on every get
(if no $digest
is already running):
if (!$rootScope.$$phase) $rootScope.$apply();
Ia addition to fetching objects from our API, our app has many directives with templateUrl
- which are fetched by angular using $http
. This causes hundreds of $digest
loops on cold start.
Commenting out the above line, reduces the number of $digest
loops to about 3, and the app runs MUCH faster, with no bindings broken (at least due to $http
not triggering a $digest
).
Question
Is there a way to disable $http triggering the $digest?
$templateCache
. (Or am I mistaking and$apply
is called even if no AJAX request has to be sent?) – Spodumene$templateCache
is implemented at a lower level and the$apply
is called even if the template is already in the cache. But looking at the sources I see this line: github.com/angular/angular.js/blob/master/src/ng/http.js#L984 IsuseApplyAsync
not doing the job? – Spodumenedone
is only called when the object is not cached and a request is sent:if (isUndefined(cachedResp)) { $httpBackend(config.method, url, reqData, done
– Gurdwara$templateCache
. I'm talking about this: npmjs.org/package/grunt-angular-templates BUT thetemplate
parameter on directives accepts a function. Check this out: plnkr.co/edit/ilffq2JkaziAOX6wUGJf?p=preview It completely bypassestemplateUrl
and a single$apply
is called. Inside it you can get the actual template from localStorage. – Spodumene$http
. – Spodumene