我只打电话给$ state.go一次。在第一次调用$ state.go之后,页面会自动刷新并调用$ stateChangeStart。我怀疑它与StateProvider配置有关。关于什么可能出错的任何指示?
这是问题的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;
}
});
}
])
答案 0 :(得分:0)
愚蠢的错误 - 如果您遇到此问题,请记住在服务器端页面上调用TemplateUrl将导致该服务器端页面呈现,这反过来将调用主页面进行呈现。
因此,如果您在母版页中定义角度脚本,则使用上述代码将会破坏它。