Angularjs 角度+;ui路由器:event.preventDefault();阻止所有应用程序状态工作
我想在应用程序启动时评估用户,如果它经过身份验证,则将状态更改为“类别”,如果不转到“土地”状态。我在Angularjs 角度+;ui路由器:event.preventDefault();阻止所有应用程序状态工作,angularjs,angular-ui-router,ionic-framework,Angularjs,Angular Ui Router,Ionic Framework,我想在应用程序启动时评估用户,如果它经过身份验证,则将状态更改为“类别”,如果不转到“土地”状态。我在run块中使用$stateChangeStart达到此目的,如下所示: .run(function($ionicPlatform, DSCacheFactory, $rootScope, $auth, $state) { $ionicPlatform.ready(function() { // Hide the accessory bar by default (remo
run
块中使用$stateChangeStart
达到此目的,如下所示:
.run(function($ionicPlatform, DSCacheFactory, $rootScope, $auth, $state) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
});
$rootScope.$on('$stateChangeStart', function (event, toState) {
if (!$auth.isAuthenticated() && toState.name !== 'land') {
event.preventDefault();
$state.go('land');
}
});
})
如果经过身份验证,就像这样:
$urlRouterProvider.otherwise('app/categories');
每件事都很好,用户得到了正确的评估,但是,点击应用程序中的任何东西,我就会进入land
状态。知道为什么吗!
路由器代码:
.config(function($stateProvider, $urlRouterProvider, $authProvider, $ionicAppProvider, API_URL) {
$stateProvider
.state('app', {
url: "/app",
abstract: true,
templateUrl: "templates/menu.html",
controller: 'AppCtrl'
})
.state('land', {
url: '/land',
templateUrl: 'templates/land.html',
controller: 'LandCtrl'
})
.state('signup', {
url: '/sign-up',
templateUrl: 'templates/sign-up.html',
controller: 'SignUpCtrl'
})
.state('signin', {
url: '/sign-in',
templateUrl: 'templates/sign-in.html',
controller: 'SignInCtrl'
})
.state('app.categories', {
url: '/categories',
views: {
'menuContent': {
templateUrl: 'templates/categories.html',
controller: 'categoriesCtrl',
}
}
});
为什么需要event.preventDefault()?如果我没有进入类别状态(另一种状态),您可以发布您的路由器代码吗?完成,很抱歉回复时间延迟。