Angular: 将所有 FormControl 与模型更改同步

Angular: synchronize all FormControls with model changes

我有一组 FormControls(通过 FormBuilder 创建):

this.someForm = this.fb.group({
     name: '',
     size: '',
     price: '',
   });

是否有任何其他方法可以从模型中更新它们(在我们的示例中 someProduct):

let someProduct = {
  name: 'shirt',
  size: 'XL',
  price: '20',
}

private updateForm(product) {
  this.someForm.controls.name.setValue(this.someProduct.name);
  this.someForm.controls.size.setValue(this.someProduct.size);
  this.someForm.controls.price.setValue(this.someProduct.price);
};

updateForm(someProduct);

特别是考虑到它可能有很多 FormControls

执行此操作的最简单方法如下

this.someForm.patchValue(this.someProduct);

Live Example