简体   繁体   English

AngularJS - 选择 False 时 ng-option 下拉列表未设置 ng-model 值

[英]AngularJS - ng-option dropdown list not setting ng-model value when False selected

I have a <select> element that uses ng-options to generate the select list and hopefully set the selected to the value of the ng-model .我有一个<select>元素,它使用ng-options生成选择列表,并希望将 selected 设置为ng-model的值。 Here is my html:这是我的 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>

My controller contains the following code:我的控制器包含以下代码:

$scope.yesNoList = [{ value: true, text: 'Y' }, { value: false, text: 'N'}];

If the IsTobacco value of the item is true, the value is set properly and everything is fine.如果该项的IsTobacco值为 true,则该值设置正确,一切正常。 However, if it is false, the required error appears, even though {{dep.IsTobacco}} renders as false.但是,如果它为 false,即使{{dep.IsTobacco}}呈现为 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.真正奇怪的部分是,如果我将列表中的选定项目更改为 true,它可以正常工作,但是如果我将其设置为 false,则会将一个新的空白项目添加到列表中并选择该空白项目。

If it seems like the issues do not lie in the lines posted above, I could gladly post more code.如果问题似乎不在上面发布的行中,我很乐意发布更多代码。

Thanks!谢谢!

EDIT: I also used chrome debugger and saw that when the data was being initiated in Angular, the value of IsTobacco was equal to false .编辑:我还使用了 chrome 调试器,看到在 Angular 中启动数据时, IsTobacco的值等于false

Edit 2: If anyone viewing this is new to AngularJS, I would definitely recommend reading these 2 articles: part 1 & part 2 .编辑 2:如果有人对 AngularJS 不熟悉,我肯定会推荐阅读这两篇文章: 第 1 部分第 2 部分 They are an overview of AngularJS forms.它们是 AngularJS 表单的概述。

The reason this happens is because a value of false gets interpreted as a lack of ngModel when used with required .发生这种情况的原因是因为false值在与required ngModel使用时被解释为缺少ngModel As discussed here , consider this excerpt from the Angular source:正如此处所讨论的,请考虑以下来自 Angular 源的摘录:

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.如果有一个 required 属性并且其值为false ,则它不会通过验证。 This is probably intentional for the sake of more easily validating required checkboxes, in which case the default value for unchecked is false .这可能是为了更轻松地验证所需的复选框,在这种情况下 unchecked 的默认值为false

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.考虑到复选框可以采用ng-true-valueng-false-value属性,可能不需要这样,但我想要么这样做,要么强制对这些属性使用非true和非false值在每个复选框上,这是更好的选择。

One workaround would be to use the strings 'true' and 'false' instead.一种解决方法是使用字符串“true”和“false”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM