Javascript 角度-在页面加载时多次调用NgForm值更改

Javascript 角度-在页面加载时多次调用NgForm值更改,javascript,angular,Javascript,Angular,我在使用ngForm的valueChanges函数时遇到问题。使用[(ngModel)]将输入变量绑定到表单时,表单在页面加载时会被多次调用 是否有一种只检测用户更改的好方法 export class ContainerComponent implements OnInit, AfterViewInit { @Input() formData: Object; @ViewChild('form') form: NgForm; constructor() { } ngOnInit

我在使用ngForm的valueChanges函数时遇到问题。使用[(ngModel)]将输入变量绑定到表单时,表单在页面加载时会被多次调用

是否有一种只检测用户更改的好方法

export class ContainerComponent implements OnInit, AfterViewInit {
  @Input() formData: Object;
  @ViewChild('form') form: NgForm;
  constructor() { }

  ngOnInit(): void {
  }

  ngAfterViewInit(): void {
     this.form.form.valueChanges.subscribe((value) => {
       //Gets called multiple times on page load
     });
  }

}

也许只检查脏/接触状态就足够了

发件人:


也许只检查脏/接触状态就足够了

发件人:

我解决了这个问题:

export class ContainerComponent implements OnInit, AfterViewInit {
  @Input() formData: Object;
  @ViewChild('form') form: NgForm;
  constructor() { }

  ngOnInit(): void {
  }

  ngAfterViewInit(): void {
     this.form.form.valueChanges.subscribe((value) => {
       if(this.form.form.dirty) {
          //DO STUFF
        }
     });
  }

}
我解决了这个问题:

export class ContainerComponent implements OnInit, AfterViewInit {
  @Input() formData: Object;
  @ViewChild('form') form: NgForm;
  constructor() { }

  ngOnInit(): void {
  }

  ngAfterViewInit(): void {
     this.form.form.valueChanges.subscribe((value) => {
       if(this.form.form.dirty) {
          //DO STUFF
        }
     });
  }

}
试试这个:

export class ContainerComponent implements OnInit, AfterViewInit {
  @Input() formData: Object;
  @ViewChild('form') form: NgForm;
  constructor() { }

  ngOnInit(): void {
   this.form.form.valueChanges.subscribe((value) => {
       //Gets called multiple times on page load
     });
  }

  ngAfterViewInit(): void {

  }

}
试试这个:

export class ContainerComponent implements OnInit, AfterViewInit {
  @Input() formData: Object;
  @ViewChild('form') form: NgForm;
  constructor() { }

  ngOnInit(): void {
   this.form.form.valueChanges.subscribe((value) => {
       //Gets called multiple times on page load
     });
  }

  ngAfterViewInit(): void {

  }

}

帮我解决了这个问题。谢谢!帮我解决了这个问题。谢谢!