Tuesday, March 5, 2019

javascript - Infinite $digest loop in the rootScope

In the rootScope of my project i have defined a function that realizes an $http call.



angular.module('rsa',['ngRoute', 'ngCookies', 'ngMaterial'])
.config(function($routeProvider, $httpProvider){
$routeProvider.when('/chapters', {
...
});

$routeProvider.when('/themes', {
...
});

// ... more routes

$routeProvider.otherwise('/home');
})
.controller(...)
.run(['$rootScope', 'UserService', '$location', '$cookies','$http', '$q',
function(rootScope, UserService, location, cookies, http, $q){
rootScope.isAdmin = function(){
return $http.get('/isAdmin').then(function(resp){...}, function(resp)
{...})
}
}])


rootScope's isAdmin method causes an infinite digest loop error.
Meanwhile, the same $http call (from isAdmin methods) works well inside other controllers.



I've read a little about what can be the problem and i found out that the $http service calls the $apply to force the $digest cycle but i don't understand what happens exactly with the $http service that drove me to get the following result in the console.



console



This is th html snippet:



 Administration 


Q1: Why is $http.get() causing an infinite number of $digest cycles.



Q2: Why does it only happen on rootScope and not in any other scope.



BTW xhr syncronous work fine and i am using angular 1.6.4

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...