'/'没有让状态传递给'/ {用户名:[A-ZA-Z0-9] {3,20}}'在AngularJs UI路由器 [英] '/' is not letting the state to pass to '/{username:[a-zA-Z0-9]{3,20}}' in AngularJs UI-router
问题描述
我有所谓的现场空URL即一个抽象根状态,其中有两个孩子的状态'profile'和'家',其中用于家庭的URL为/和配置文件的URL为/ {用户名:[A-ZA-Z0-9] {3,20}}'序来测试我试过很多次去的轮廓状态,但它总是到达家乡
但是,当我修改家乡
URL与说/家
那么我能去在的国企
。在网上搜索后,我找不到这个问题。它是一个错误吗?或者说我做错了什么?
下面是code为 $ stateProvider
$ stateProvider。
状态('网站',{
网址:'',
摘要:真实,
观点:{
'':{
templateUrl:意见/ site.html'//这是基本的模板
},
导航栏@网站':{
templateUrl:意见/ navbar.html
}
},
数据:{
访问:access.public
}
})。
状态('site.home',{
网址:'/',
templateProvider:['$ rootScope,$ HTTP,$州',函数($ rootScope,$ HTTP,$州){
//console.log($state);
VAR模板='查看/ main_new.html';
如果($ rootScope.User.isLoggedIn()){
模板='查看/ new_room_me.html
}
返回$ http.get(模板)。然后(功能(RESP){
返回resp.data;
});
}]
})。
状态('site.profile',{
网址:'/ {用户名:[A-z0-9A-Z] {3,20},严格的:真正}
templateUrl:意见/ new_room_friend.html
});
$ urlRouterProvider.otherwise('/ ANKIT');
有一个工作的例如,在这里我主要是切换状态声明的顺序:
.STATE('site.profile',{
网址:'/ {用户名:[A-z0-9A-Z] {3,20}}',
...
})
.STATE('site.home',{
网址:'/',
...
})
原因是, UI路由器
URL引擎在迭代状态,以便他们申报,并试图找到一个URL匹配。这样一来,我们就得到了工作定义。
不过,我建议:
使用一些关键字来区分URL,例如
网址:'/型材/ {用户名:[A-z0-9A-Z] {3,20}}',
块引用>这不仅将帮助引擎找到的网址正确的状态,但即使从URL的用户/读者的角度 - 这在我看来更明确,自我描述。
总之,开关状态的清晰度应该在这里解决这个问题...
I have a abstract root state called 'site' with empty url i.e. '' , which has two child state 'profile' and 'home' where the url for home is '/' and the url for profile is '/{username:[a-zA-Z0-9]{3,20}}' inorder to test I tried many times to go to the profile state but it always reaches the home state.
But when i change the
home state
url with say'/home'
then i'm able to go to theprofile state
. After searching the web i couldn't find this issue. Is it a bug? or i'm doing something wrong?Here is the code for
$stateProvider
$stateProvider. state('site', { url: '', abstract: true, views: { '': { templateUrl: 'views/site.html' //this is the base template }, 'navbar@site': { templateUrl:'views/navbar.html' } }, data:{ access: access.public } }). state('site.home', { url: '/', templateProvider :['$rootScope','$http','$state',function($rootScope,$http,$state){ //console.log($state); var template = 'views/main_new.html'; if($rootScope.User.isLoggedIn()){ template = 'views/new_room_me.html' } return $http.get(template).then(function (resp) { return resp.data; }); }] }). state('site.profile',{ url:'/{username:[a-z0-9A-Z]{3,20},strict:true}', templateUrl:'views/new_room_friend.html' }); $urlRouterProvider.otherwise('/ankit');
解决方案There is a working example, where I mostly switched the order of state declaration:
.state('site.profile',{ url:'/{username:[a-z0-9A-Z]{3,20}}', ... }) .state('site.home', { url: '/', ... })
The reason is that
ui-router
url engine does iterate states in order they were declared, and tries to find a url match. this way, we will get the working definition.But I would suggest:
use some keyword to distinguish url, e.g.
url:'/profile/{username:[a-z0-9A-Z]{3,20}}',
Not only this will help the engine to find the proper state by url, but even from perspective of the user/reader of the url - this seems to me more explicit, self-descriptive.
Anyhow, switching state def should solve the issue here...
这篇关于'/'没有让状态传递给'/ {用户名:[A-ZA-Z0-9] {3,20}}'在AngularJs UI路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!