ui-router的 $stateChangeStart事件里$stage.go()死循环
$stage.go()直接死循环, 一直在stateChangeStart,页面直接崩溃。
app.js
define(['routes', 'util', 'services/Interceptor', 'angular-ui-router'], function (config, util, Interceptor) {
var app = angular.module('app', ['ui.router', 'ng-progress']);
app.config(
[
'$locationProvider',
'$controllerProvider',
'$compileProvider',
'$filterProvider',
'$provide',
'$stateProvider',
'$urlRouterProvider',
'$httpProvider',
'$httpProgressOpsProvider',
function ($locationProvider, $controllerProvider, $compileProvider, $filterProvider, $provide, $stateProvider, $urlRouterProvider, $httpProvider, $httpProgressOpsProvider) {
app.controller = $controllerProvider.register;
app.directive = $compileProvider.directive;
app.filter = $filterProvider.register;
app.factory = $provide.factory;
app.service = $provide.service;
//$locationProvider.html5Mode(true);
if (config.states !== undefined) {
angular.forEach(config.states, function (states, path) {
$stateProvider.state(states.name, states.data);
});
}
// if (config.defaultRoutePath !== undefined) {
// $urlRouterProvider.otherwise({ redirectTo: config.defaultRoutePath });
// }
$urlRouterProvider.otherwise('/');
//异常拦截器
$httpProvider.interceptors.push(Interceptor.exception);
//http请求拦截,进度条
$httpProgressOpsProvider.setOps({
background: '#6435c9',
startAt: 30,
autoPauseAt: 75,
//http : false,
//increment : 0.5,
//duration : 100
});
}
]);
app.run(['$rootScope', '$location', '$state', function ($rootScope, $location, $state) {
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
//判断登录
if (!$rootScope.user) {
//取消默认跳转行为
event.preventDefault();
//跳转
$state.go('app.warn_rule_set');
}
console.log(event, fromState, toState, $state);
$rootScope.currentStateDesc = toState.desc || null;
});
}])
return app;
});
route.js路由配置
define(['services/DependencyResolver'], function (dependencyResolverFor) {
var loadController = function (controllerName) {
return ["$q", function ($q) {
var deferred = $q.defer();
require([controllerName], function () {
deferred.resolve();
});
return deferred.promise;
}];
};
return {
defaultRoutePath: '/',
states: [
{
name: "app",
data: {
url: '/',
views: {
'': {
templateUrl: '/views/common/wrapper.html',
controller: 'MainController'
},
'content@app': {
templateUrl: '/views/common/content.html',
controller: null
}
},
resolve: {
MainController: loadController("controllers/MainController")
}
}
},
{
name: "app.warn_rule_set",
data: {
url: 'warn_rule_set',
desc: '告警规则/设置',
views: {
'content': {
templateUrl: '/views/warn/rule_set.html',
controller: 'WarnRuleSetController',
controllerAs: 'vm'
}
},
resolve: {
WarnRuleSetController: loadController("controllers/WarnRuleSetController")
}
}
},
{
name: "app.warn_rule_list",
data: {
url: 'warn_rule_list',
desc: '告警规则/列表',
views: {
'content': {
templateUrl: '/views/warn/rule_list.html',
controller: 'WarnRuleListController',
controllerAs: 'vm'
}
},
resolve: {
WarnRuleListController: loadController("controllers/WarnRuleListController")
}
}
},
{
name: "app.warn_rule_detail",
data: {
url: 'warn_rule_detail',
desc: '告警规则/详情',
params: { pWarnRule: null },
views: {
'content': {
templateUrl: '/views/warn/rule_detail.html',
controller: 'WarnRuleDetailController',
controllerAs: 'vm'
}
},
resolve: {
WarnRuleDetailController: loadController("controllers/WarnRuleDetailController")
}
}
},
{
name: "app.warn_sms_set",
data: {
url: 'warn_sms_set',
desc: '短信告警/设置',
views: {
'content': {
templateUrl: '/views/warn/sms_set.html',
controller: 'SmsWarnSetController',
controllerAs: 'vm'
}
},
resolve: {
SmsWarnSetController: loadController("controllers/SmsWarnSetController")
}
}
},
{
name: "app.warn_sms_list",
data: {
url: 'warn_sms_list',
desc: '短信告警/列表',
views: {
'content': {
templateUrl: '/views/warn/sms_list.html',
controller: 'SmsWarnListController',
controllerAs: 'vm'
}
},
resolve: {
SmsWarnListController: loadController("controllers/SmsWarnListController")
}
}
},
{
name: "app.warn_sms_detail",
data: {
url: 'warn_sms_detail',
desc: '短信告警/详情',
params: { pSmsWarn: null },
views: {
'content': {
templateUrl: '/views/warn/sms_detail.html',
controller: 'SmsWarnDetailController',
controllerAs: 'vm'
}
},
resolve: {
SmsWarnDetailController: loadController("controllers/SmsWarnDetailController")
}
}
},
{
name: "app.warn_mail_set",
data: {
url: 'warn_mail_set',
desc: '邮件告警/设置',
views: {
'content': {
templateUrl: '/views/warn/mail_set.html',
controller: 'MailWarnSetController',
controllerAs: 'vm'
}
},
resolve: {
MailWarnSetController: loadController("controllers/MailWarnSetController")
}
}
},
{
name: "app.warn_mail_list",
data: {
url: 'warn_mail_list',
desc: '邮件告警/列表',
views: {
'content': {
templateUrl: '/views/warn/mail_list.html',
controller: 'MailWarnListController',
controllerAs: 'vm'
}
},
resolve: {
MailWarnListController: loadController("controllers/MailWarnListController")
}
}
},
{
name: "app.warn_mail_detail",
data: {
url: 'warn_mail_detail',
desc: '邮件告警/详情',
params: { pMailWarn: null },
views: {
'content': {
templateUrl: '/views/warn/mail_detail.html',
controller: 'MailWarnDetailController',
controllerAs: 'vm'
}
},
resolve: {
MailWarnDetailController: loadController("controllers/MailWarnDetailController")
}
}
},
{
name: "app.query_component",
data: {
url: 'query_component',
desc: '查询关联/组件',
views: {
'content': {
templateUrl: '/views/query/component.html',
controller: 'QueryComponentController',
controllerAs: 'vm'
}
},
resolve: {
QueryComponentController: loadController("controllers/QueryComponentController")
}
}
},
{
name: "app.query_core",
data: {
url: 'query_core',
desc: '查询关联/内核',
views: {
'content': {
templateUrl: '/views/query/core.html',
controller: 'QueryCoreController',
controllerAs: 'vm'
}
},
resolve: {
QueryCoreController: loadController("controllers/QueryCoreController")
}
}
},
{
name: "app.query_software",
data: {
url: 'query_software',
desc: '查询关联/软件',
views: {
'content': {
templateUrl: '/views/query/software.html',
controller: 'QuerySoftwareController',
controllerAs: 'vm'
}
},
resolve: {
QuerySoftwareController: loadController("controllers/QuerySoftwareController")
}
}
},
{
name: "app.query_integrity",
data: {
url: 'query_integrity',
desc: '查询关联/完整性',
views: {
'content': {
templateUrl: '/views/query/integrity.html',
controller: 'QueryIntegrityController',
controllerAs: 'vm'
}
},
resolve: {
QueryIntegrityController: loadController("controllers/QueryIntegrityController")
}
}
},
{
name: "app.query_attack",
data: {
url: 'query_attack',
desc: '查询关联/攻击事件',
views: {
'content': {
templateUrl: '/views/query/attack.html',
controller: 'QueryAttackController',
controllerAs: 'vm'
}
},
resolve: {
QueryAttackController: loadController("controllers/QueryAttackController")
}
}
},
]
};
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$stateChangeStart
会捕获所有的路由变化事件吧,你这里是在$rootScope注册的事件,所有在$rootScope的路由变化均会捕获,你把跳转逻辑写到hook里肯定,肯定死循环了。解决方法就是不要在$rootScope上注册,在专门的控制权限的scope里面注册吧,另外建议升级到ui-router 1.x 的版本,关于$state这类的hook,官方文档已经建议使用$transitions服务代替了,因为它更灵活,可以满足一些$state hook满足不了的情况。