【问题标题】:How to check an ngModel input field is dirty?如何检查 ngModel 输入字段是否脏?
【发布时间】:2019-01-02 22:41:02
【问题描述】:

我有这个 HTML 模板:

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

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

现在我的 my.component.ts 文件中只有这个基本代码:

export class UriFieldComponent implements OnInit {
    constructor() { }

    ngOnInit() { }

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

【问题讨论】:

    标签: angular angular7 dirty-checking


    【解决方案1】:

    NgModel 类中有一个 dirty 属性。这意味着您可以这样做:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-23
      • 1970-01-01
      • 2021-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多