如何使用angularjs初始化reactive表单

qq24tv8q  于 2023-09-30  发布在  Angular
关注(0)|答案(1)|浏览(124)

我有一个angularjs的React形式,并尝试初始化几个字段:

initializeBusinessEposForm(): void {
    this.businessEposForm = this.formBuilder.group({
      custom_pos_priority: new FormControl(false),
      custom_float_menu: new FormControl(false),
      epos_quick_code_print: new FormControl(false),
      permission: this.formBuilder.group({
        isEposCustomerData: new FormControl(false),
        stripe_terminal_processor: new FormControl(''),
        tips: [],
      }),
    });
  }

这里的tips是一个数组,包含以下值:

[
    {
        "name": "gratuity_tip",
        "isActive": false,
        "orderType": []
    },
    {
        "name": "terminal_tip",
        "isActive": true,
        "orderType": [
            1,
            4
        ]
    }
]

现在如何将此数组作为数组添加到tips: []

ygya80vv

ygya80vv1#

你试过使用FormArray吗?

initializeBusinessEposForm(): void {
    this.businessEposForm = this.formBuilder.group({
      custom_pos_priority: false,
      custom_float_menu: false,
      epos_quick_code_print: false,
      permission: this.formBuilder.group({
        isEposCustomerData: false,
        stripe_terminal_processor: '',
        tips: this.formbuilder.array(tips.map(tip => initTipFormGroup(tip)),
      }),
    });
  }

initTipFormGroup(tip: Tip): FormGroup {
  return this.formbuilder.group({
    name: tip.name,
    isActive: tip.isActive,
    orderType: tip.orderType,
  });
}

相关问题