当$validators返回false时,用ng-model-options删除作用域变量
scope variable is deleted with ng-model-options when $validators return false
我的问题是这样的。
这很简单……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>
在你身上消失了。没有要显示的值
相关文章:
- 从 Javascript 中设置 ng-model name
- Toggle Switch在包含ng model和ng repeat之后不进行切换
- 在Angularjs中,我如何使用ng repeat、ng model和ng click来动态更改内容
- 当与之关联的 ng-model 为 false 时,Angularjs 复选框值不会取消选中
- ng-model 绑定到选择标签生成的空默认值
- 对象不能在angularjs ng-model中访问
- 为什么 HTML 中的 Value 属性在与 AngularJS 中的 ng-model 一起使用时不起作用
- ng-repeat不绑定ng-model对象数组
- AngularJS:ng-model 绑定在使用 jQuery 更改时不会更新
- 选择/选项 ng-model 未绑定 ng-selected
- Angular ng-model 将数据作为数组发送到 rails-api
- 为什么 AngularJS 在使用 ng-model 时不会在文本框上设置长度属性
- AngularJS:Ng:init-如何分配Ng:model
- 在指令中使用 ng-model 和 ng-repeat
- 使用 angularjs 访问控制器中的 ng-model 数据
- 如何在 Angularjs 中为 ng-model 增加价值
- 如何在 AngularJS 中将 ng-model 值检索到控制器
- 从角度“ng-model”选择元素中选择第一个选项,带有一个按钮
- AngularJS选择带有ng-model和ng-repeat的默认值,而不是ng-option
- Angular ng-option:迭代一个数字数组,以显示另一个数组的内容,但在ng-model中选择了索引