Angular 在值更改时更新表单控件值

Angular updates form control value on value change

我可以订阅 formControl.valueChanges 以查看设置为 formControl 的新值。但是现在如果我需要在订阅中进一步更新这个新值,你会怎么做呢?即当 formControl 值更改时,我需要进一步更新它,以便 formControl 具有该值的更新版本。

// not working code
this.formControl.valueChanges.subscribe(value => this.formControl.value = value + " changed");

如果我们想在 setValue 调用时停止发射 valueChanges。我们可以将选项配置设置为setValue方法

this.formControl.valueChanges.subscribe(value => {
  this.formControl.setValue(`${value} changed`,{emitEvent:false});
 }