gpt4 book ai didi

javascript - $rootScope :infdig error in angularjs application

转载 作者:行者123 更新时间:2023-12-03 07:37:57 27 4
gpt4 key购买 nike

用户登录到我们的应用程序后,用户凭据就会存储到本地存储中。如果用户在已经登录的情况下路由到登录状态,则会在控制台中显示以下错误:

Uncaught Error: [$rootScope:infdig] http://errors.angularjs.org/1.3.10/$rootScope/infdig?p0=10&p1=%5B%5D

检查本地存储并重定向用户的代码

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {

// Grab the user from local storage and parse it to an object
var user = JSON.parse(localStorage.getItem('user'));

// If there is any user data in local storage then the user is quite
// likely authenticated.

if(!user) {
if(toState.name !== 'login.signin'){
//go to application "login" state
$state.go('login.signin');
event.preventDefault();
}
}
else{
// The user's authenticated state gets flipped to
// true so we can now show parts of the UI that rely
// on the user being logged in
$rootScope.authenticated = true;

// Putting the user's data on $rootScope allows
// us to access it anywhere across the app.
// Here we are grabbing what is in local storage
$rootScope.currentUser = user;


if(toState.name === "login.signin") {

// go to the application "dashboard" state
$state.go('app.dashboard');

// Preventing the default behavior allows us to use $state.go
// to change states
event.preventDefault();
}
}
});

我的解决方案是

Changing $stateChangeStart with $stateChangeSuccess

最佳答案

当您的摘要循环运行时,您无法更改任何范围变量,我认为您使用$timeout。在您定义的地方注入(inject) $timeout

$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
$timeout(function(){
var user = JSON.parse(localStorage.getItem('user'));

// If there is any user data in local storage then the user is quite
// likely authenticated.

if(!user) {
if(toState.name !== 'login.signin'){
//go to application "login" state
$state.go('login.signin');
event.preventDefault();
}
}
else{
// The user's authenticated state gets flipped to
// true so we can now show parts of the UI that rely
// on the user being logged in
$rootScope.authenticated = true;

// Putting the user's data on $rootScope allows
// us to access it anywhere across the app.
// Here we are grabbing what is in local storage
$rootScope.currentUser = user;


if(toState.name === "login.signin") {

// go to the application "dashboard" state
$state.go('app.dashboard');

// Preventing the default behavior allows us to use $state.go
// to change states
event.preventDefault();
}
}
});

关于javascript - $rootScope :infdig error in angularjs application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35533878/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com