AngularJS - 选择假时NG选项下拉列表中没有设置NG-模型值 [英] AngularJS - ng-option dropdown list not setting ng-model value when False selected

查看:182
本文介绍了AngularJS - 选择假时NG选项下拉列表中没有设置NG-模型值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个<选择>使用 NG-optiona 来生成选择列表,并希望设置元素所选的 NG-模式的价值。这里是我的html:

I have a <select> element that uses ng-optiona to generate the select list and hopefully set the selected to the value of the ng-model. Here is my html:

<td style="text-align:center">
    <ng-form name="IsTobaccoForm">
        <select name="Input" required ng-model="dep.IsTobacco" ng-options="item.value as item.text for item in yesNoList"></select>{{dep.IsTobacco}}
        <span class="alert-error" ng-show="IsTobaccoForm.Input.$error.required"><strong>*Required</strong></span>
    </ng-form>
</td>

我的控制器包含以下code:
$ scope.yesNoList = [{值:真,文本:'Y'},{值:false,文本:'N'}];

如果该项目的 IsTobacco 值为true,该值设置正确,一切都很好。但是,如果是假的,出现所需的错误,即使 {{} dep.IsTobacco} 呈现为假。真正奇怪的是,如果我在列表中真正的改变所选择的项目,它工作正常,但是如果我把它设置为false,一个新的空白项添加到列表和空白项被选中。

If the IsTobacco value of the item is true, the value is set properly and everything is fine. However, if it is false, the required error appears, even though {{dep.IsTobacco}} renders as false. The really weird part is that if I change the selected item in the list to true, it works fine, however if I set it to false, a new blank item is added to the list and that blank item is selected.

如果它看起来像问题不在于上面贴的线条,我可以很高兴地发布更多code。

If it seems like the issues does not lie in the lines posted above, I could gladly post more code.

谢谢!

编辑:我也用镀铬调试器和看到,当数据在角正开始, IsTobacco 的值等于

I also used chrome debugger and saw that when the data was being initiated in angular, the value of IsTobacco was equal to false.

编辑2:如果有人观看,这是新AngularJS,我肯定会推荐阅读这些文章2:的第1部分&安培; 第2部分。它们的AngularJS形式概述

Edit 2: If anyone viewing this is new to AngularJS, I would definitely recommend reading these 2 articles: part 1 & part 2. They are an overview of AngularJS forms.

推荐答案

之所以出现这种情况是因为值获得跨preTED为缺乏 ngModel 使用时需要。随着讨论的这里,从源头角度考虑这个摘录:

The reason this happens is because a value of false gets interpreted as a lack of ngModel when used with required. As discussed here, consider this excerpt from the Angular source:

var validator = function(value) {
    if (attr.required && (isEmpty(value) || value === false)) {
        ctrl.$setValidity('required', false);

如果有一个必需的属性,它的值是,它没有通过验证。这可能是更容易验证所需的复选框的缘故故意的,在这种情况下为未选中的默认值为

If there is a required attribute and its value is false, it does not pass validation. This is probably intentional for the sake of more easily validating required checkboxes, in which case the default value for unchecked is false.

这可能并不需要这样,考虑到复选框可以采取 NG-真值 NG-假值属性,但是我想这是不是这样做或执行使用非 - 真正和非 - 对于每一个复选框的属性值,这是更好的选择。

It may not need to be this way, considering that checkboxes can take ng-true-value and ng-false-value attributes but I guess it was either do this or enforce the use of non-true and non-false values for those attributes on every checkbox, and this was the better choice.

一个解决办法是使用字符串真与假来代替。

One workaround would be to use the strings 'true' and 'false' instead.

这篇关于AngularJS - 选择假时NG选项下拉列表中没有设置NG-模型值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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