当$validators返回false时,用ng-model-options删除作用域变量

scope variable is deleted with ng-model-options when $validators return false

本文关键字:ng-model-options 删除 作用域 变量 validators 返回 false      更新时间:2023-09-26

我的问题是这样的。
这很简单……angular似乎遇到了一个问题。

http://plnkr.co/edit/5diCIhFoWX8FneSSBkfA?p =

预览

描述:

我使用ng-model-options,这样模型只有在输入上有一个模糊事件时才会更新。

<input type="text" ng-model="myText" ng-model-options="{ updateOn : 'blur' }">

这工作得很好,但是当我添加一个指令来触发$验证器时,myText范围变量在$validators返回false时消失。

<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;更改输入并创建blur事件。

编辑
新砰砰作响:
http://plnkr.co/edit/5diCIhFoWX8FneSSBkfA?p=preview

我认为它应该在ng-repeat中触发问题,但事实并非如此。

这似乎是Angularjs中正确的行为,这意味着你想做什么呢?

这里有几个选项…

通过添加allowInvalid: true到你的选项,将呈现你的文本被渲染,但然后你否定验证。

查看更新的柱塞链接

ng-model-options="{ updateOn : 'blur', allowInvalid: true }"

试图在Angular代码库中找到$validators,也许这与正确的行为有关。

code base $validtors

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>

在你身上消失了。没有要显示的值