[英]How should I use Angular UI Router?
I have searched the Internets for a couple of days, but can't find what I'm looking for. 我已经在互联网上搜索了几天,但找不到我想要的东西。
I am building an SPA using angularjs but I'm struggling with something I think should be quite basic. 我正在使用angularjs构建SPA,但是我在尝试一些我认为应该很基础的事情。 Coming from a ASP.NET MVC
mindset, I am trying to set up routes for listing data and creating new data, for example: 来自ASP.NET MVC
心态,我试图设置用于列出数据和创建新数据的路由,例如:
.state('placements', {
abstract: true,
url: '/placements',
template: '<div ui-view></div>'
})
.state('placements.list', {
url: '',
templateUrl: mapPathToViews('placements.list'),
controller: 'PlacementController'
})
.state('placements.new', {
url: '/new',
templateUrl: mapPathToViews('placements.new'),
controller: 'PlacementController'
})
So with these states set up, I would think that navigating to /placements/new
would give me the "new placements" view. 因此,在设置了这些状态后,我认为导航到/placements/new
将使我获得“新的展示位置”视图。
This works perfectly if I use the ui-sref navigation 如果我使用ui-sref导航,这将完美工作
The problem comes from when I type myapp.com/placements/new
into a browser and hit GO, no route is matched at all. 问题出在我在浏览器中键入myapp.com/placements/new
并单击GO时,根本没有匹配的路由。 However, if I go to myapp.com/placements
I see the correct view as expected. 但是,如果我访问myapp.com/placements
,则会看到正确的视图。
My question is this: Am I right in how I think the router should be set up or do I need to have a completely separate state for new placements, such as myapp.com/newplacement
? 我的问题是:我对路由器的设置方式是否正确,还是需要对myapp.com/newplacement
等新放置位置具有完全独立的状态?
It seems to be something to do with the way ui-router figures out the state on first page load. 这似乎与ui-router确定第一页加载状态的方式有关。 If I load the page with the url myapp.com/placements/new
, then click on a navigation item which points to a state with the url of '/'
(meaning home page, right?) then the url becomes myapp.com/placements/
and the view for the home page is rendered. 如果我使用URL myapp.com/placements/new
加载页面,则单击导航项,该状态指向URL为'/'
(意味着首页,对吗?),则该URL变为myapp.com/placements/
和主页视图。 Similarly, if I click another nav link pointing to '/somewhereelse'
the url becomes myapp.com/placements/somewhereelse
and the somewhereelse
view is rendered. 同样,如果我点击指向另一个导航链接'/somewhereelse'
的网址将成为myapp.com/placements/somewhereelse
和somewhereelse
视图渲染。 I'm guessing then that when it loads /placements/new
it (for some reason) is actually looking for a root state with the url of /new
which doesn't exist. 我猜然后,当它加载/placements/new
时(出于某种原因)实际上正在寻找带有/new
的url的根状态,该URL不存在。
This is proven when I add $urlRouterProvider.otherwise('/not-found');
当我添加$urlRouterProvider.otherwise('/not-found');
时,证明了这一点$urlRouterProvider.otherwise('/not-found');
and reload the page at /placements/new
, the url becomes myapp.com/placements/not-found
. 然后在/placements/new
处重新加载页面,该网址将变为myapp.com/placements/not-found
。
Is this a bug? 这是错误吗? (sorry, undocumented feature) or have I got something wrong here? (抱歉,未记录的功能)还是我在这里出了点问题?
Ok, this is doing my head in now. 好的,这就是我的想法。
I figured I'd have a go at hacking something to get around this, and thought I'd get away with adding a /new
state and just calling $state.transitionTo('placements.new')
and all will be well. 我认为我可以尝试破解一些东西来解决这个问题,并认为我可以添加一个/new
状态并只调用$state.transitionTo('placements.new')
,一切都会好起来的。 Turns out that if I do that, the url becomes myapp.com/placements/placements/new
- wtf? 原来,如果我这样做,则该URL变为myapp.com/placements/placements/new
?
Found it! 找到了!
I changed what I was searching for and found this SO answer which lead me to this issue . 我更改了要搜索的内容,并找到了这个答案 ,这使我遇到了这个问题 。 Long story short, I added <base href="/" />
to my index.html and voila, all is working well. 长话短说,我在index.html中添加了<base href="/" />
,瞧,一切都很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.