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

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

问题描述

我正在将Angular与UI-router一起使用,我希望用户始终(或在大多数情况下,为简洁起见,我们可以总是说总是")进入步骤1,即我的主页上的Web应用程序.

UI路由器配置简写:

.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'),但是有一种方法可以只设置一个入口点,并且只有从步骤1,而无需进行重定向的费用?
预先感谢

解决方案

要解决此问题,您还可以使用$ stateChangeStart事件.

$rootScope.$on('$stateChangeStart',
function(event, toState, toParams, fromState, fromParams){
  if (fromState.name === '' && toState.name !== 'state1'){
    event.preventDefault();
    $state.go('state1');
  }
});

来源:

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'
    };
    .
    .
    .
    .
}

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

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

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

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

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