根据某些条件直接重定向到页面
Directly redirect to a page based on some condition
我已经有一个 home
页面,现在我想添加一个由 localStorage
变量 alreadyShown
控制的 homePlus
页面。设置是,homePlus
第一次显示后,我们将alreadyShown
设置为true
,这样以后所有homePlus
的加载都会直接重定向到home
页。我有以下代码:
.state('homePlus', {
url: '/homePlus',
templateUrl: '/htmls/homePlus.html',
controller: 'homePlusCtrl',
resolve: {
checkAlready: ['$window', function ($window) {
if ($window.localStorage['alreadyShown'] === "true") {
$window.location.href = "https://localhost:3000/home"
}
}]
}
})
上面的代码确实检查了 alreadyShown
。然而,即使 alreadyShown
是 true
,在重定向到 home
之前,它仍然显示 homePlus
页面 0.5-1
秒。理想情况下,在这种情况下我根本不想看到 homePlus
页面。
有人知道如何实现吗?
ui-路由器事件 $stateChangeStart
应该有效。
angular.module.run
app.run(['$rootScope' function ($rootScope) {
$rootScope.$on('$stateChangeStart', function (event, to, toParams, from, fromParams) {
...do something
});
}]);
我已经有一个 home
页面,现在我想添加一个由 localStorage
变量 alreadyShown
控制的 homePlus
页面。设置是,homePlus
第一次显示后,我们将alreadyShown
设置为true
,这样以后所有homePlus
的加载都会直接重定向到home
页。我有以下代码:
.state('homePlus', {
url: '/homePlus',
templateUrl: '/htmls/homePlus.html',
controller: 'homePlusCtrl',
resolve: {
checkAlready: ['$window', function ($window) {
if ($window.localStorage['alreadyShown'] === "true") {
$window.location.href = "https://localhost:3000/home"
}
}]
}
})
上面的代码确实检查了 alreadyShown
。然而,即使 alreadyShown
是 true
,在重定向到 home
之前,它仍然显示 homePlus
页面 0.5-1
秒。理想情况下,在这种情况下我根本不想看到 homePlus
页面。
有人知道如何实现吗?
ui-路由器事件 $stateChangeStart
应该有效。
angular.module.run
app.run(['$rootScope' function ($rootScope) {
$rootScope.$on('$stateChangeStart', function (event, to, toParams, from, fromParams) {
...do something
});
}]);