如何检查ngModel输入字段是否脏? [英] How to check an ngModel input field is dirty?

查看:61
本文介绍了如何检查ngModel输入字段是否脏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个HTML模板:

I have this HTML template:

<input type="text"
  ngModel
  #myValue="ngModel"
  name="{{ fieldName }}"
  id="{{ fieldName }}"
  value="{{ myVal }}"
  class="form-control"
  (change)="checkDirty(myValue)">

如何检查my.component.ts文件中的该字段是否脏?

How do I check this field is dirty in my.component.ts file?

现在,我的my.component.ts文件中只有以下基本代码:

Now I have only this basic code in my.component.ts file:

export class UriFieldComponent implements OnInit {
    constructor() { }

    ngOnInit() { }

    checkDirty(value) {
        // in here I need to check is dirty or not
    }
}

推荐答案

NgModel类具有属性.这意味着您可以:

NgModel class has an dirty property in it. This mean you can do:

checkDirty(value) {
    if (value.dirty) {
      ...
    }
}

但是,您使用它的方式是不正确的,如果您在输入更改时检查了dirty属性,则很明显该输入是肮脏的!.换句话说,如果您要使用 checkDirty 函数,则输入总是脏的.

But, the way you use it is incorrect, if you check the dirty property on input change it is obvious that the input is dirty!. In other words, if you're getting to your checkDirty function the input is always dirty.

这篇关于如何检查ngModel输入字段是否脏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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