我在使用 AngularJS 1.5.8 的应用程序中遇到了奇怪的行为:
plunker (https://plnkr.co/edit/zaHVJeK8hdxk2gaOL9Pf?p=preview) 和视频 (http://recordit.co/eszvfdfC9S)
- 步骤 1。 最初更改
ng-required
不会调用ng-change
函数 - 步骤 2。 在更改
input
并删除它们后(input
为空),ng-required
会调用ng-change
函数
期望的行为?
- 步骤 2。 在更改
input
并删除它们后(input
为空),ng-required
不应该调用ng-change
函数。就像最初一样,以及当输入具有某些值时
请告诉我这是不是一个 bug。如果不是,为什么更改 ng-required
不总是或根本不调用 ng-change
?
答案已找到-------------------------------------------------------------------------------------------------------
NgModelController 有两个属性:$viewValue(由用户输入的值)和 $modelValue(绑定到模型的值)。当用户输入的值未通过验证时,NgModelController 将模型设置为 undefined。
在 AngularJS 1.3 中,他们添加了 ng-model-options 指令。它允许您配置何时以及如何更新模型。您可以使用 allowInvalid 选项防止将模型设置为 undefined:
ng-model-options="{allowInvalid: true}"