问题描述
我是新来的有角的学生.我的 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
:
由于您在问题中使用了 $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.
使用$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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!