0
我只調用$ state.go一次。在第一次調用$ state.go之後,頁面會自動刷新並調用$ stateChangeStart。我懷疑它與StateProvider配置有關。任何關於什麼可能是錯誤的指針?Ui路由器FromState總是^,無限循環
這裏的問題的一個GIF,我覺得代碼如下罪魁禍首:http://i.giphy.com/3o85xGdxHnkanVecgM.gif
angular.module('ticketingSystemApp', ['ui.router', 'ngCookies', 'ticketingSystemApp.filters', 'ticketingSystemApp.directives', 'ticketingSystemApp.controllers', 'ticketingSystemApp.services'])
.run(['$state', '$rootScope', function ($state, $rootScope) {
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
alert("ToState=" + toState.name + " FromState=" + fromState.name + " url=" + fromState.Url);
if (($rootScope.username == null || $rootScope.password == null) && toState.name !== "login") {
$state.go("login");
e.preventDefault();
} else if ($state.name === "") {
$state.go('userpanel');
e.preventDefault();
}
});
}
]);
.config([
'$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider) {
$locationProvider.hashPrefix('!').html5Mode(true);
// UI States, URL Routing & Mapping. For more info see: https://github.com/angular-ui/ui-router
// ------------------------------------------------------------------------------------------------------------
$stateProvider
.state('login', {
url:'/login',
views: {
"mainContent": {
templateUrl: '/Account/Login'
}
}
})
.state('userpanel', {
url:'/userpanel',
views: {
"mainContent": {
templateUrl: '/UserPanel'
}
}
});
//We don't know where we are. Go to user panel.
$urlRouterProvider.otherwise(function ($injector, $location) {
var $state = $injector.get('$state');
if ($state.name === undefined) {
$state.go('userpanel');
return;
}
});
}
])