在我的网络应用程序中使用angular-ui-router进行状态更改。下面是我的route.config文件。
路由config.js
$stateProvider
.state('login', {
url: '/',
views:{
pageContent:{
templateUrl: 'Login/login_default.html',
controller: 'loginController'
},
footer:{
templateUrl: 'common/footer.html',
controller: 'footerController'
}
},onEnter: function(){
// if(true){ $state.go('dashboard')}
},resolve:{
dependencies:['$ocLazyLoad', function($ocLazyLoad) {
return $ocLazyLoad.load(['ui.bootstrap','ngMessages','headerCtrl','footerCtrl']);
}],
loginController:['$q', '$ocLazyLoad','dependencies',function($q, $ocLazyLoad,dependencies) {
var deferred = $q.defer();
require.ensure(['./styles/login/login.less'], function (require) {
var mod = require('./Login');
$ocLazyLoad.load({
name: mod.name,
});
deferred.resolve(mod.controller);
});
return deferred.promise;
}]
},public:true
})
.state('Password', {
url: '/Password?token',
views:{
header:{
templateUrl: 'common/header.html',
controller:"headerController"
},
pageContent:{
templateUrl: 'ForgotPassword/forgotPassword.html',
controller: 'forgotPasswordCtrl'
},
footer:{
templateUrl: 'common/innerFooter.html',
controller: 'footerController'
}
},
resolve:{
dependencies:['$ocLazyLoad', function($ocLazyLoad) {
return $ocLazyLoad.load(['headerCtrl','ui.bootstrap']);
}],
forgotPasswordCtrl:['$q','$ocLazyLoad','dependencies', function($q,$ocLazyLoad,dependencies) {
var deferred = $q.defer();
require.ensure(['./styles/forgotPassword/forgotPassword.less'], function (require) {
var mod = require('./ForgotPassword');
$ocLazyLoad.load({
name: mod.name,
});
deferred.resolve(mod.controller);
});
return deferred.promise;
}]
},public:true
});
在index.js文件中检查$ stateChange,如下所示。
run(function($http,$rootScope,$cookieStore,$location,$window,$state) {
$rootScope.token = $http.defaults.headers.common['SessionID'];
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
let loggedIn = $cookieStore.get('success');
if(!toState.public && !loggedIn){
$state.go('login');
event.preventDefault();
}
});
});
下面是点击密码按钮的Html页面,它应该去忘记密码页面,或者如果我在URL上键入密码,它应该进入密码页面。
<div class="inp-wrapper frgtpwd">
<a style="text-decoration:none;color:#000;" ui-sref="Password" class="password">FORGOT PASSWORD ?</a>
</div>
我尝试了一切,但无法解决此问题。如果我将检查$ statechangeStart函数中的toState.name
并尝试转到密码页面,那么它将继续循环并抛出类似Maximum call stack
的错误(类似这样)。