$ locationPath导致错误:[$ rootScope:inprog] [英] $locationPath leads to Error: [$rootScope:inprog]
问题描述
我要在我的角度JS的webapp切换视图。
I need to switch view in my angular js webapp.
为了处理路由,我现在用的$ stateProvider,如下:
In order to handle the routing, I am using the $stateProvider, as here:
.config(['$httpProvider', '$stateProvider', '$urlRouterProvider', function ($httpProvider, $stateProvider, $urlRouterProvider) {
$stateProvider
.state('common', {
templateUrl: 'assets/html/template.html',
abstract: true,
})
.state('login', {
url: '/login',
parent: 'common',
templateUrl: 'app/models/authentication/views/login.html',
controller: 'LoginController'
})
.state('home', {
url: '/',
parent: 'common',
templateUrl: 'app/models/home/views/home.html',
controller: 'HomeController'
})
...
在我登录控制器,我想切换视图(从登录首页)
Inside my login controller, I am trying to switch the view (from Login to Home)
.controller('LoginController', ['$scope', '$rootScope', '$cookieStore', '$location', '$window', '$http',
function ($scope, $rootScope, $cookieStore, $location, $window, $http) {
$scope.login = function () {
loadHttp();
};
function loadHttp() {
var url = "http://myurl";
...
.then(function (response) {
$scope.$apply(function () {
$location.path('/home');
console.log("$location.path: " + $location.path);
});
});
}
但是,当我到达的 $ location.path('/家庭'); 的我得到这个错误:
Error: [$rootScope:inprog] http://errors.angularjs.org/1.4.8/$rootScope/inprog?p0=%24digest
at Error (native)
at http://localhost:3000/adminTool/assets/js/angular.min.js:6:416
at t (http://localhost:3000/adminTool/assets/js/angular.min.js:126:132)
at r.$apply (http://localhost:3000/adminTool/assets/js/angular.min.js:133:515)
at http://localhost:3000/adminTool/app/models/authentication/controllers.js:46:32
at http://localhost:3000/adminTool/assets/js/angular.min.js:119:129
at r.$eval (http://localhost:3000/adminTool/assets/js/angular.min.js:133:313)
at r.$digest (http://localhost:3000/adminTool/assets/js/angular.min.js:130:412)
at r.$apply (http://localhost:3000/adminTool/assets/js/angular.min.js:134:78)
at g (http://localhost:3000/adminTool/assets/js/angular.min.js:87:444)
我是什么做错了吗?如何摆脱它,最后切换视图?
What am I doing wrong? How to get rid of it and finally switch view?
我读到使用$从适用<一个href=\"http://stackoverflow.com/questions/11784656/angularjs-location-not-changing-the-path\">SO
PS:我是很新的角度。
PS: I am very new to angular
推荐答案
您得到的错误是因为 $范围。$适用
围绕 $ location.path('/家')
语句。角已经处于消化循环,使再次触发认为循环中(通过调用 $适用
)会给你这个错误。删除 $范围。$适用
将为此可能解决您的问题。
The error you get is because of the $scope.$apply
around the $location.path('/home')
statement. Angular is already in a digest loop so triggering it again within that loop (by calling $apply
) will give you this error. Removing the $scope.$apply
will therefor probably fix your problem.
摘要循环已经触发的原因是因为你可能正在使用它采用了承诺返回值 $ HTTP
服务。解决这一承诺时,角始终触发摘要。
The reason the digest loop is already triggered is because you are probably using the $http
service which uses a promise to return the value. Angular always triggers a digest when resolving this promise.
但是,除了你probaly要使用 $状态
服务移动到另一种状态,而不是使用 $位置移动到另一个位置
服务。 $ state.go('家')
很可能是你在找什么。
But aside from that you probaly want to use the $state
service to move to another state instead of moving to another location using the $location
service. $state.go('home')
would probably be what you are looking for.
这篇关于$ locationPath导致错误:[$ rootScope:inprog]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!