否则对于具有类型参数的状态

Otherwise for states with typed parameters

我开始在路由配置中使用类型化参数(例如 {id:int})。如果一个状态由于类型 而与 不匹配,otherwise 似乎不会触发。

例如:

$stateProvider
    .state('stuff', {
        url: '/version/{version:int}/stuff',
        templateUrl: // ...,
        controller: // ...
    })
    .state('list', {
        url: '/list',
        // ...
    });

$urlRouterProvider.otherwise('/list');

如果版本是 "x"(不是整数),我不会被重定向到列表页面。相反,我最终出现在一个空白页面上,控制台中没有任何错误。

我该如何解决这个问题?

(澄清:我在地址栏中输入无效的 URL 来测试这个,而不是使用 ui-sref。)

更新: 我相信我已经找到了导致它的原因(感谢@RadimKöhler 提供的示例消除了一些可能性)。看我的 plunk.

一切正常 除了 的 URL 无法匹配使用 $urlMatcherFactoryProvider.type() 构建的自定义类型。我根据 .

的答案使用了 guid 类型

我可以通过在 URL 中使用正则表达式来解决这个问题,但最好能有一个修复。

更多信息:pattern 添加到自定义 Type object 似乎可以解决此问题。该模式必须省去锚点 (^$),所以类似于:

pattern: /[a-f\d]{8}-(?:[a-f\d]{4}-){3}[a-f\d]{12}/i

我会说,您想要的概念正在发挥作用。有a working example

此状态 def (1:1 提问)

$stateProvider
  .state('stuff', {
    url: '/version/{version:int}/stuff',
    templateUrl: 'tpl.html',
    controller: 'StuffCtrl',
  })
  .state('list', {
    url: '/list',
    templateUrl: 'tpl.html',
    controller: 'ListCtrl',
  });


$urlRouterProvider.otherwise('/list');

将妥善处理这些链接:

// list
<a href="#/list">

// ui-sref
<a ui-sref="stuff({version:1})">
<a ui-sref="stuff({version:2})">

// href
<a href="#/version/12/stuff">

这些将不起作用(如预期的那样) - 因此它们不会创建任何 href (具有此类参数的状态不存在)

<a ui-sref="stuff({version:'A'})">
<a ui-sref="stuff({version:'xyz'})">

这些 href 将被重定向到 otherwise

<a href="#/version/x/stuff">
<a href="#/version/AB/stuff">

实际查看 here