简体   繁体   English

如何检查ngModel输入字段是否脏?

[英]How to check an ngModel input field is dirty?

I have this HTML template: 我有这个HTML模板:

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

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: 现在,我的my.component.ts文件中只有以下基本代码:

export class UriFieldComponent implements OnInit {
    constructor() { }

    ngOnInit() { }

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

NgModel class has an dirty property in it. NgModel类具有属性。 This mean you can do: 这意味着您可以:

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

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!. 但是,您使用它的方式是不正确的,如果您在输入更改中检查了dirty属性,则很明显该输入是肮脏的! In other words, if you're getting to your checkDirty function the input is always dirty. 换句话说,如果要使用checkDirty函数,则输入始终为脏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM