什么是正确的使用方式"布尔"参数类型与UI的路由器? [英] What is the correct way to use the "bool" param type with ui-router?

查看:139
本文介绍了什么是正确的使用方式"布尔"参数类型与UI的路由器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图利用参数类型的UI路由器的,似乎无法让他们的权利。

I have been trying to make use of the param types with ui-router and can't seem to get them right.

$ stateProvider.state({
    名称:'home.foo',
    网址:'/富/ {isBar:BOOL}
    控制器:功能(){},
    templateUrl:foo.html
    });

我的期望是,我应该能像这个过渡到状态:

My expectation is that I should be able to transition to that state like this:

$ state.go(home.foo,{isBar:假})

UI-SREF =home.foo({isBar:假})

但是在生成$ stateParams你会看到isBar:真正的

however in the resulting $stateParams you will see isBar: true

综观方式布尔参数类型写入我想真/假的应该是连接codeD为0/1,在URL上,但这种情况不会发生。如果PARAMS为$使用0/1 state.go那么它的工作原理,是德codeD假/真实的,但进一步混淆不一样,如果使用UI-SREF工作的问题。

Looking at the way the 'bool' param type is written I suppose true/false should be encoded as 0/1 on the url but this doesn't happen. If use 0/1 in the params for $state.go then it works and is decoded as false/true but to further confuse the issue this doesn't work if using the ui-sref.

希望这 plunker 将解释它更好。任何提示AP preciated!

Hopefully this plunker will explain it better. Any hints appreciated!

编辑:我在使用布尔参数类型的目标是在$ stateParams

My goal in using the bool param type is to end up with a boolean data type in $stateParams

推荐答案

有一个更新,并与工作plunker 布尔 自定义类型

在的情况下,我们愿与一个布尔以工作像类型,
预计,并接受:

In case, we would like to work with a bool like type, which expects and accepts the:

真正 0 1

true, false, 0, 1

我们只需要注册我们的自定义类型:

we just have to register our custom type:

app.config(['$urlMatcherFactoryProvider', function($urlMatcherFactory) {

  $urlMatcherFactory.type('boolean',
    // our type custom type
    {
     name : 'boolean',
     decode: function(val) { return val == true ? true : val == "true" ? true : false },
     encode: function(val) { return val ? 1 : 0; },
     equals: function(a, b) { return this.is(a) && a === b; },
     is: function(val) { return [true,false,0,1].indexOf(val) >= 0 },
     pattern: /bool|true|0|1/
    })

}]);

然后我们可以使用这个网址确定指标的任何国家的内部:

And then we can use this url defintion inside of any state:

...
, url: '/foo/{isBar:boolean}'
...

注:为什么布尔?没有布尔?因为布尔已被注册为 0 | 1 据我记得

NOTE: why boolean? not bool? Because bool is already registered for 0|1 as far as I remember

检查一下这里

原始

简单的解决方案在这个更新的plunker 以串工作,

simple solution working with "strings" in this updated plunker,

... // states definitions
, url: '/foo/{isBar:(?:bool|true|0|1)}'

这篇关于什么是正确的使用方式"布尔"参数类型与UI的路由器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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