且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AngularJS - 选择假时NG选项下拉列表中没有设置NG-模型值

更新时间:2023-02-01 11:20:15

之所以出现这种情况是因为值获得跨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.