这是我的问题的根源。
很简单……看来 Angular 遇到了问题。
http://plnkr.co/edit/5diCIhFoWX8FneSSBkfA?p=preview
描述:
我正在使用 ng-model-options,以便模型仅在输入上存在模糊事件时更新。
<input type="text" ng-model="myText" ng-model-options="{ updateOn : 'blur' }">
这工作正常,但是当我添加指令来触发 $validators 时,当 $validators
返回 false 时,myText
作用域变量就会消失。
<input type="text" ng-model="myText" ng-model-options="{ updateOn : 'blur' }" check-errors>
指令
.directive('checkErrors', function(){
return {
require : 'ngModel',
link : function(){
ngModel.$validators.validHour = function(modelValue){
return true;
};
}
};
});
要查看问题,请将 return true;
更改为 return false;
更改输入并创建模糊事件。
编辑
新的笨蛋:
http://plnkr.co/edit/5diCIhFoWX8FneSSBkfA?p=preview
我认为应该在 ng-repeat 内触发该问题,但事实并非如此。
最佳答案
这似乎是 Angularjs 中的正确行为,这意味着您想做什么?
这里有一些选项...
通过将 allowInvalid: true 添加到您的选项中,将呈现正在呈现的文本,但随后您将否定验证。
查看更新后的插件 link
ng-model-options="{ updateOn : 'blur', allowInvalid: true }"
更新
尝试在 Angular 代码库中查找 $validators,也许这与其正确行为有关。
ctrl.$$runValidators(modelValue, viewValue, function(allValid) {
// If there was no change in validity, don't update the model
// This prevents changing an invalid modelValue to undefined
if (!allowInvalid && prevValid !== allValid) {
// Note: Don't check ctrl.$valid here, as we could have
// external validators (e.g. calculated on the server),
// that just call $setValidity and need the model value
// to calculate their validity.
ctrl.$modelValue = allValid ? modelValue : undefined;
看起来只有当模型通过验证时 ctrl.$modalValue 才会设置为更新,或者它变得未定义,这就是为什么你的
<pre> {{ appCtrl.test }} </pre>
正在从你身上消失。没有任何展示值(value)。
关于javascript - 当 $validators 返回 false 时,作用域变量将被 ng-model-options 删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31595970/