Error: Maximum call stack size exceeded on Angular Ui Router State Change(错误:Angular Ui 路由器状态更改时超出了最大调用堆栈大小)
问题描述
基本上我要做的是根据用户身份验证状态更改状态.
Basically what I'm trying to do is change the state depending on user authentication status.
当我在没有 $rootScope 的情况下执行 $state.go();
时,我可以在没有错误的情况下重定向到页面,但是当我使用
When I do $state.go();
without $rootScope, I can redirect to the page without the error but when I do with
$rootScope.$on('$stateChangeStart', function (event) {$state.go();event.preventDefault()})
以下是控制器代码:
DiaryDashboard.controller('AppController',
['$scope', '$rootScope', '$localStorage', '$http', '$state'
, function ($scope, $rootScope, $localStorage, $http, $state) {
$rootScope.$on('$stateChangeStart', function (event, toState) {
if ($localStorage.userdetails &&
$localStorage.userdetails.isAuthenticated == true) {
//Check for Authentication state
//If not redirect to the state in the else clause
//Populate the $rootScope with user details
$rootScope.userdetails = $localStorage.userdetails;
//Switch to the parent state
$state.go();
event.preventDefault();
} else {
//If User not authenticated
//GO to the Authentication State
$state.go('auth');
event.preventDefault();
}
})
}]);
推荐答案
这里最重要的是要明白这一点:
The most important thing here is to understand this:
如果不需要,请不要重定向.换句话说,如果用户已经被重定向到预期状态 - 我们应该离开......有 一个工作plunker 具有类似的解决方案.
Do not redirect if not needed. Other words, if user is already redirected to intended state - we should leave... There is a working plunker with similar solution.
查看此问答答:
调整后的代码:
$rootScope.$on('$stateChangeStart', function (event, toState) {
var isNavigatingToAuth = toState.name === "Auth";
if(isNavigatingToAuth){
return; // no need to redirect
}
if ($localStorage.userdetails &&
$localStorage.userdetails.isAuthenticated == true)
...
这篇关于错误:Angular Ui 路由器状态更改时超出了最大调用堆栈大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:错误:Angular Ui 路由器状态更改时超出了最大调用堆栈大小


- 失败的 Canvas 360 jquery 插件 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Fetch API 如何获取响应体? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 400或500级别的HTTP响应 2022-01-01