设置多个控制器在一个状态,IONIC
问题描述:
我想检查是否有用户登录,因为它重定向到另一个页面,如果没有用户,我想重定向到homePage。主页有一个名为storeCtrl的控制器。设置多个控制器在一个状态,IONIC
我尝试这样做:
.state('homePage',
{
url: '/homePage',
templateUrl: 'templates/homePage/homePage.html',
controller:'storeCtrl',
function ($localStorage,$state) {
if ($localStorage.user) {
$state.go('tabs.profile');
} else {
$state.go('homePage');
}
}
})
,但它不工作。我想这也:
.state('homePage',
{
url: '/homePage',
templateUrl: 'templates/homePage/homePage.html',
controller:
function ($localStorage,$state) {
if ($localStorage.user) {
$state.go('tabs.profile');
} else {
$state.go('homePage'),
controller:storeCtrl
}
}
})
但也没有工作。
答
而不是将其置于该状态。尝试添加这里面app.run
app.run(function($ionicPlatform,$state,$rootScope,$localStorage) {
$ionicPlatform.ready(function() {
//your code
})
$rootScope.$on('$stateChangeStart', function (event,next, nextParams, fromState,fromParams) {
var nextScreen=next.name;
if (nextScreen!="homePage" && $localStorage.user===undefined) {
event.preventDefault();
$state.go('homePage');
return;
}
});
})
是引发RangeError:最大调用堆栈大小超过 错误 – noor
最大调用堆栈大小才会来,如果有一个循环..那就是如果在根上的每次改变它在if块内部。尝试做一些调试。此外,我添加了state.go –
后返回我试过,它不会返回错误,但它不起作用。我登录并保存了用户,但是当我刷新页面时,即使用户仍然保存,它仍然重定向到主页而不是简介页面 – noor