AngularJS什么时候使用UI Rout [英] AngularJS When and Otherwise with UI Rout

查看:90
本文介绍了AngularJS什么时候使用UI Rout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的有角学生.我的UI路线的路线有问题.

I'm new angular student. I have a problem with my routes of UI Route.

这是我的代码:

  $urlRouterProvider
    .when('/GRN', '/GRN/Produtos')
    .when('/Executivo', "Executivo/Produtos")
    .otherwise("/NotFound")

我想这样做:

When -> /GRN/SOME_WRONG_LINK -> go to /GRN/Produtos
When -> /Executivo/SOME_WRONG_LINK -> go to /Executivo/Produtos
When -> /SOME_WRONG_LINK -> Go to NotFound

基本上,我希望用户是否开始输入正确的URL(在本例中为GRN或Executivo),他会转到此链接的一个主页,而不会转到"NotFound",因为他从一个正确的链接开始.

Basically i want if the user start putting right URL (in this case GRN or Executivo), he goes to one main page of this link, and no to "NotFound", because he starts with a right link.

有人可以帮我吗?

非常感谢!

推荐答案

使用 $ routeProvider :

Using $routeProvider:

由于您在问题中使用了 $ routeProvider ,因此

Since you used $routeProvider in your question,

您可以使用

.c.run(function($rootScope,$location,$state) {

$rootScope.$on( '$stateChangeStart', function(e,toState,toParams,fromState,fromParams) {

    if( $location.path().search('GRN') > -1)
    {
        $state.go('GRN_Produtos');
        event.preventDefault();     
    }

    if( $location.path().search('Executivo') > -1)
        $state.go('Executivo_Produtos');
        event.preventDefault();     
    }

    $urlRouterProvider.otherwise("/");

});    

}); onfig(['$ routeProvider',function($ routeProvider){$ routeProvider

});onfig(['$routeProvider', function($routeProvider) { $routeProvider

  .when('/GRN',{redirectTo:'/GRN/Produtos'})  

  .when('/Executivo',{redirectTo:'/Executivo/Produtos'})  

  .otherwise({redirectTo: '/NotFound'}); 
}])

重定向到:

{(string | Function)} =>值,用于更新$ location路径并触发路由重定向.

{(string|Function)} => value to update $location path with and trigger route redirection.

此处是od redirectTo的参考

使用 $ stateProvider :

Using $stateProvider:

假设您的状态如下,

.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('GRN_Produtos', {
            url: '/GRN/Produtos',
            templateUrl: '',
            controller: ''
        })
        .state('Executivo_Produtos', {
            url: '/Executivo/Produtos',
            templateUrl: '',
            controller: ''
        })
      .state('otherwise', {
            url: '/notfound',
       });

    }])

现在您可以在运行块中重定向

Now you can redirect in the run block,

  .run(function($rootScope,$location,$state) {

    $rootScope.$on( '$stateChangeStart', function(e,toState,toParams,fromState,fromParams) {

        if( $location.path().search('GRN') > -1)
        {
            $state.go('GRN_Produtos');
            event.preventDefault();     
        }

        else if( $location.path().search('Executivo') > -1)
            $state.go('Executivo_Produtos');
            event.preventDefault();     
        }
        else
        {
            $state.go('otherwise');
            event.preventDefault();     
        }


    });    
});

这篇关于AngularJS什么时候使用UI Rout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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