AngularJS NG-值布尔验证 [英] AngularJS ng-value boolean validation

查看:742
本文介绍了AngularJS NG-值布尔验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要具体:我有两个单选按钮和NG-模型值必须是布尔值。由于这是不可能与普通的HTML值属性,我发现有用Angularjs NG-值。问题是,当我想要做一些验证,当选择单选按钮NG值=假是它认识到它作为一个空的NG-模型,我们有验证错误。

To be specific: I have two radio buttons and their ng-model value must be boolean. Since this is not possible with normal html value property I found useful Angularjs ng-value. Problem is when I want to do some validation, when radio button ng-value="false" is selected it recognizes it as an empty ng-model and we have validation error.

示例 http://plnkr.co/编辑/ lvdNHZoSSN1nM6uLyc0Q?p = preVIEW

任何线索如何解决呢?

推荐答案

这是一个错误。

值=真/假不会起作用,因为你的无线输入将不会被初始化,并加载模型时,核对无误,但 NG-值=真/假将无法工作,因为假的选择将导致表无效。

value="true/false" won't work because your radio inputs won't be initialized and checked correctly when your model is loaded, but ng-value="true/false" won't work because "false" selections will cause the form to be invalid.

我有同样的问题,报告说,它作为一个问题,但由于难以预料何时会解决,我也想出了一个解决方法:的 NG-布尔无线电

I was having the same problem and reported it as an issue, but since there's no telling when it will be resolved, I also came up with a workaround: ng-boolean-radio

这基本上是模型的布尔真/假值转换成字符串true/假的价值观,匹配形式的字符串,值=真/假属性。这将使相应的单选输入默认情况下,当加载模型进行检查。它也将让您保存假值,因为假!= FALSE

It basically converts your model's boolean true / false values into string "true" / "false" values, which match your form's string value="true/false" attributes. This will allow the corresponding radio inputs to be checked by default when the model loads. It will also allow you to save "false" values because "false" != false.

最后,$ P $通过转换字符串pserves布尔数据类型真/假值回布尔真/假保存他们在模型前。

Finally, it preserves the boolean datatype by converting the string "true" / "false" values back to boolean true / false before saving them in the model.

我希望帮助!

这篇关于AngularJS NG-值布尔验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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