简体   繁体   English

角度为ID的UI-Router嵌套路由

[英]Angular UI-Router nested route with id

I've got two routes in my app, nested into each other with ui-router: 我的应用程序中有两条路由,与ui-router相互嵌套:

app/locations/locations.js 应用程序/位置/ locations.js

'use strict';

angular.module('treasuremapApp')
  .config(function ($stateProvider) {
    $stateProvider
      .state('locations', {
        url: '/locations',
        templateUrl: 'app/locations/locations.html',
        controller: 'LocationsCtrl'
      });
  });

and app/locations/location/location.js 和app / locations / location / location.js

'use strict';

angular.module('treasuremapApp')
  .config(function ($stateProvider) {
    $stateProvider
      .state('location', {
        url: '/locations/:id',
        templateUrl: 'app/locations/location/location.html',
        controller: 'LocationCtrl'
      });
  });

This works fine flawless most of the time, but when I first on my list of locations, click on one, it gets displayed just fine, even the back button works, but when I then just type /locations into the URL bar (any other way without the back button), the router seems to try to open the "lower" route with controller and template but of course is missing the ID from the url then. 在大多数情况下,这种方法都可以正常工作,但是当我第一次在位置列表中单击时,它就可以很好地显示,即使后退按钮也可以使用,但是当我然后在URL栏中键入/locations (任何其他方法)没有后退按钮的方式),路由器似乎尝试使用控制器和模板打开“下级”路由,但是当然会丢失网址中的ID。

I have the feeling my understanding of the ui-router is a bit flawed, so can someone please explain to me, what I am doing wrong here. 我感觉我对ui路由器的理解有些缺陷,所以有人可以向我解释一下,我在这里做错了什么。

Perhaps you entered /locations/ into the URL instead of /locations . 也许您在URL中输入了/locations/而不是/locations Entering /locations/ will trigger the second route, because the ui-router would expect that :id equals (empty) . 输入/locations/将触发第二条路由,因为ui-router会期望:id等于(empty)

Also look this FAQ if you want it to behave differently: https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-make-a-trailing-slash-optional-for-all-routes 如果您希望它的行为不同,也请查看此FAQ: https : //github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-make-a-trailing-slash-optional-换全路由

How can I have my routes be activated when the url contains either a trailing slash or not? 网址中是否包含斜杠时如何激活我的路由?

Set strictMode to false in your config block: 在配置块中将strictMode设置为false:

$urlMatcherFactoryProvider.strictMode(false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM