【问题标题】:Why to use FormBuilder instead of FormGroup?为什么要使用 FormBuilder 而不是 FormGroup?
【发布时间】:2019-07-31 22:30:33
【问题描述】:
While creating a reactive forms in angular we why we need to use formBuilder instead of FormGroup when we can make reactive forms using either of them.
Here i also provided what i am trying to ask 

maintenanceForm: FormGroup;

ngOnInit() {
 this.maintenanceForm = new FormGroup({
      'value': new FormControl(null),
      'dropdown': new FormControl(null)
    })
}

/* 这里因为我们可以用这两种方式制作反应式表单,但是什么时候使用它们中的任何一种 */

this.maintenanceForm = this.formBuilder.group({
        body_type_id: ['', Validators.required],
        make_name: ['', Validators.required]
      });

【问题讨论】:

    标签: angular7 angular-reactive-forms


    【解决方案1】:

    根据我的经验,使用 FormBuilder 可以减少要编写的语法,尤其是当您有非常大和复杂的表单时。请看下面的链接,

    https://angular.io/api/forms/FormBuilder https://angular.io/api/forms/FormGroup

    此链接显示了使用这两种方法的语法差异 https://angular.io/guide/reactive-forms

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-04
      • 2021-02-20
      • 2013-12-07
      • 2015-05-03
      • 2011-10-09
      • 2013-09-13
      • 2010-11-18
      • 1970-01-01
      相关资源
      最近更新 更多