[英]stop user to go login page when user logged in using ionic
I want that if a user is logged in he can't go to the login and OTP page by hardware back button. 我希望用户登录后无法通过硬件后退按钮进入登录和OTP页面。
.controller('viewoemCtrl', function($rootScope,$scope,$http,$ionicPopup,$state,$ionicHistory,$ionicLoading,productService,$ionicPlatform) {
$scope.user = {}; //declares the object user
$ionicPlatform.onHardwareBackButton(function () {
if (true) { // your check here
$ionicPopup.confirm({
title: 'Exit from App!',
template: 'are you sure you want to exit?'
}).then(function (res) {
if (res) {
navigator.app.exitApp();
}
})
}
})
})
You don't need to check the click on the back button, because that itself will call a state transition from ui-router. 您无需检查单击后退按钮,因为它本身将调用ui-router的状态转换。 just register a callback to the
$stateChangeStart
event fired by ui-router
and control there if the user is logged in and if the target state is login, in this case you prevent the transition. 只需注册一个由
ui-router
触发的$stateChangeStart
事件的回调,并在其中控制用户是否登录以及目标状态是否为登录,在这种情况下,可以防止过渡。 This will handle any case of state transition (the back button, a direct link from the menu etc.) 这将处理任何状态转换的情况(后退按钮,菜单中的直接链接等)
$rootScope.$on('$stateChangeStart', function(event, toState, toParams,
fromState, fromParams){
//userIsLogged is a flag you should retrieve from your code
if(userIsLogged && toState === 'login'){
event.preventDefault();
}
})
take a look at the documentation 看一下文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.