How to set value in array form with Angular

前端 未结 3 1324
情深已故
情深已故 2021-01-01 01:57

I want to set value in array like this:

this.form.controls[name].setValue(\'name\')

but I am working w

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-01 02:30

    Here is what I have done to set value manually in formArray's specific form control and worked for me. I have formArray named as bundleDetails.

       this.formBuilderObj['bundleDetails'] = 
       this.formBuilder.array([this.createBundleItem()]);
    
        createBundleItem(): FormGroup {
        return this.formBuilder.group({
         bsku: ['', Validators.required],
         bqty: ['', Validators.required],
         bprice: ['', Validators.required]
        });
       }
    

    Method to set bsku control's value (where index is [formGroupName]="i" passed from html file).

       setSku(sku: string, index: number) {
       const faControl = 
       (this.pmForm.controls['bundleDetails']).at(index);
       faControl['controls'].bsku.setValue(sku);
      }
    

    Hope this helps. Happy coding.

提交回复
热议问题