否则,对于类型参数状态 [英] Otherwise for states with typed parameters

查看:140
本文介绍了否则,对于类型参数状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用输入参数(如 {ID:INT} )在我的路由配置。如果一个国家不匹配的因为类型,否则不出现触发。

例如:

  $ stateProvider
    .STATE('东西',{
        网址:'/版本/ {版本:INT} /东西',
        templateUrl:// ...,
        控制器:// ...
    })
    .STATE(名单',{
        网址:'/名单,
        // ...
    });$ urlRouterProvider.otherwise('/名单');

如果该版本是X(不是int),我不是重定向到列表页面。相反,我结束了在控制台中没有错误的空页上。

我该如何解决这个问题?

(澄清:我在地址栏键入无效的URL来测试这个,不使用 UI-SREF

更新:我相信我已经找到了导致它(感谢@RadimKöhler对于消除了一些可能性的例子)。看到我的普拉克

一切正常的除了对于不匹配 $ urlMatcherFactoryProvider.type建立了一个自定义类型的网址()。我用了一个的GUID 类型的基础上的答案,<一个href=\"http://stackoverflow.com/questions/30475790/how-to-add-guid-regex-in-angular-ui-router-state\">this问题。

我可以解决此通过在URL中使用正规的前pressions,但它会是不错的解决方法。

更多信息:添加模式到自定义的类型的对象似乎解决这​​个问题。该模式已离开了锚( ^ $ ),所以是这样的:

 模式:/ [A-F \\ D] {8}  - (?:[A-F \\ D] {4}  - ){3} [A-F \\ D] {12} / I


解决方案

我说,这个概念是你想要的,是工作。有工作的例子

此状态下高清的(1:1的问题)

  $ stateProvider
  .STATE('东西',{
    网址:'/版本/ {版本:INT} /东西',
    templateUrl:tpl.html',
    控制器:'StuffCtrl',
  })
  .STATE(名单',{
    网址:'/名单,
    templateUrl:tpl.html',
    控制器:'的ListCtrl',
  });
$ urlRouterProvider.otherwise('/名单');

会妥善处理这些链接:

  //名单
&LT; A HREF =#/列表&GT;// UI的SREF
&LT;一个UI的SREF =的东西({版本:1})&GT;
&LT;一个UI的SREF =的东西({版本:2})&GT;// HREF
&LT; A HREF =#/版本/ 12 /东西&GT;

这是行不通的(如预期)的 - 所以他们不会产生任何的href的(这样PARAMS这种状态不存在)

 &LT;一个UI的SREF =的东西({版本:'A'})&GT;
&LT;一个UI的SREF =的东西({版本:'某某'})&GT;

这些 的href 会被重定向到其它

 &LT; A HREF =#/版本/ X /东西&GT;
&LT; A HREF =#/版本/ AB /东西&GT;

检查它在行动这里

I started using typed parameters (such as {id:int}) in my route configuration. If a state doesn't match because of the type, otherwise does not appear to trigger.

For example:

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

$urlRouterProvider.otherwise('/list');

If the version is "x" (not an int), I am not redirected to the list page. Instead I end up on an empty page with no errors in the console.

How can I fix this?

(Clarification: I'm typing in invalid URLs in the address bar to test this, not using ui-sref.)

Update: I believe I've found what's causing it (thanks to @RadimKöhler for an example that eliminated some possibilities). See my plunk.

Everything works fine except for URLs that fail to match a custom type built with $urlMatcherFactoryProvider.type(). I used a guid type based on the answers to this question.

I can work around this by using regular expressions in the URL, but it would be nice to have a fix.

More info: Adding pattern to the custom Type object appears to fix this. The pattern has to leave out the anchors (^$), so something like:

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

解决方案

I'd say, that the concept as you wanted, is working. There is a working example

this state def (1:1 to question)

$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');

will properly handle these links:

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

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

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

these will not work (as expected) - so they will not created any href (such state with such params does not exist)

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

these href will be redirect to otherwise

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

Check it in action here

这篇关于否则,对于类型参数状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆