Angular UI 路由器设置网站入口点.重定向到主页 URL [英] Angular UI-router set website entry point. Redirect to home URL

查看:34
本文介绍了Angular UI 路由器设置网站入口点.重定向到主页 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 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');
  }
});

来源:https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-create-rules-to-prevent-access-to-a-state

这篇关于Angular UI 路由器设置网站入口点.重定向到主页 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆