JustDoItTomorrow
3/7/2018 - 11:12 PM

响应式表单:setValue和patchValue

// 什么时候设置呢? 答案取决于组件何时得到数据模型的值

ngOnChanges()
  // setValue可以设置所有值
  this.heroForm.setValue({
    name:    this.hero.name,
    address: this.hero.addresses[0] || new Address()
  });
}

// patchValue可以设置一部分
this.heroForm.patchValue({
  name: this.hero.name
});

ngOnChanges() {
  // reset除了设置还会重置状态
  this.heroForm.reset({
    name: this.hero.name,
    address: this.hero.addresses[0] || new Address()
  });
}