1

我使用响应式表单并尝试重置 5 个特定字段,但我的表单会监听任何更改并发送请求。如果我重置 5 个字段,我的表单会发送 5 个请求。但我只想在所有更改后发送一个请求。

this.subs = form.get('city').valueChanges.subscribe(city => {
            form.get('property').reset(null, {
              emitEvent: false,
              onlySelf: true
            });

            form.get('cars').reset(null, {
              emitEvent: false,
              onlySelf: true
            });

            form.get('tariff').reset(null, {
              emitEvent: false,
              onlySelf: true
            });

            form.get('version').reset(null, {
              emitEvent: false,
              onlySelf: true
            });
          });

我做错了什么?

PS:我使用 ngx-formly 来组织表单。

4

1 回答 1

0

超出您提供的内容一定是导致问题的原因。像这样的东西很好用:

 form = new FormGroup({
    a: new FormControl('a'),
    b: new FormControl('b'),
    c: new FormControl('change me'),
  });

  ngOnInit() {
    this.subs = [
      this.form.valueChanges.subscribe(v => console.log('form value change', v)),
      this.form.get('c').valueChanges.subscribe(() => { 
        console.log('reset');

        this.form.get('a').reset('d', { emitEvent: false });
        this.form.get('b').reset('e', { emitEvent: false });
      }),
    ];
  }

这是一个有效的堆栈闪电战: https ://angular-awifgk.stackblitz.io

于 2019-04-22T19:08:51.277 回答