问题描述
我将 Angular 与 UI-router 结合使用,我希望用户始终(或在大多数情况下,但为简洁起见,我们可以说始终")在我的主页第 1 步进入网络应用.
I'm using Angular with UI-router and I want the user to always (or under most conditions, but for brevity we can say "always") enter the web app on step1, my home page.
UI-router 配置简写:
UI-router config shorthand:
.config(function($stateProvider, $urlRouterProvider) {
// route to root if no valid route found
$urlRouterProvider.otherwise('/step1');
var step1 = {
name : 'step1',
url : '/step1',
templateUrl: 'partials/step1.html'
};
var step2 = {
name : 'step2',
url : '/step2',
templateUrl: 'partials/step2.html'
};
.
.
.
.
}
我可以做一个解析或指定一个控制器并简单地设置 $location.path('/step1'),但是有没有一种方法可以只设置一个入口点并让 step2/step3 urls 只能在从step1,无需重定向成本?
提前致谢
I can do a resolve or specify a controller and simply set $location.path('/step1'), but is there a way to just set one entry point and have the step2 / step3 urls be only reachable after starting from step1, without the cost of a redirect?
Thanks in advance
推荐答案
也可以使用 $stateChangeStart 事件来解决这个问题.
To solve this problem you can also use $stateChangeStart event.
$rootScope.$on('$stateChangeStart',
function(event, toState, toParams, fromState, fromParams){
if (fromState.name === '' && toState.name !== 'state1'){
event.preventDefault();
$state.go('state1');
}
});
这篇关于Angular UI 路由器设置网站入口点.重定向到主页 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!