看在ngModel变化。美元的角度指令无效 [英] watching for changes in ngModel.$invalid in angular directive

查看:76
本文介绍了看在ngModel变化。美元的角度指令无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个指令,它取代了选择元素自定义输入控件。下面是它的一个简化版本:

I have a directive which replaces a select element with a custom input control. Here's a simplified version of it:

angular.module('MyModule', []).directive('reflector', function($timeout) {
    return {
        require: 'ngModel',
        link: function(scope, element, attrs, ngModel) {
            element.insertAfter('<input type=text id="new-' + attrs.id + '" />');
            element.hide()
        }
    };

});

我想这个自定义输入控件,以反映原来的选择元素,也就是的有效/无效状态添加NG-无效上课的时候​​基本元素是无效的。

I'd like this custom input control to reflect the valid/invalid state of the original select element, i.e. add the ng-invalid class when the base element is invalid.

有没有办法收看更改为 ngModel $无效?我知道我可以做范围。$腕表(attrs.ngModel,...),但给我的模型数据,而不是表单元素的有效/无效状态..

Is there any way to watch for changes to ngModel.$invalid? I know I can do scope.$watch(attrs.ngModel, ...), but that gives me the model data, not the form element's valid/invalid state..

推荐答案

您可以观看所有的属性从的 ngModelController

You can watch all the attributes from the ngModelController:

$scope.$watch(function(){return ngModel.$invalid;},function(newVal,oldVal){ ...

ngModel 设置以下CSS类到元素:NG-有效,NG-无效,NG-脏,NG-质朴

And ngModel sets the following css classes onto the element: ng-valid, ng-invalid, ng-dirty, ng-pristine.

这篇关于看在ngModel变化。美元的角度指令无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆