我有这样的代码,在这里我设置选项{allowInvalid: true}
<input type="number" name="userName"
ng-model="user.name"
ng-model-options="{ allowInvalid: true }"
ng-blur="test(user)"/>
当我将非数字值输入非数字时,例如:
abc
,为什么
user.name
仍然更改为undefined
?我期望模型
user.name
应该是'abc'
。完整代码:http://plnkr.co/edit/BsLk2GWVkchNgKaOV453?p=preview
我正在使用 Angular v1.3.16
最佳答案
在link之后,您不能将allowInvalid
与type="number"
属性一起使用。
Issues with HTML5 constraint validation
In browsers that follow the HTML5 specification,
input[number]
does not work as expected withngModelOptions.allowInvalid
. If a non-number is entered in the input, the browser will report the value as an empty string, which means the view / model values in ngModel and subsequently the scope value will also be an empty string.
关于javascript - Angular ngModelOptions allowInvalid无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30683673/