How to set value in array form with Angular

前端 未结 3 1299
情深已故
情深已故 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条回答
  • 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 = 
       (<FormArray>this.pmForm.controls['bundleDetails']).at(index);
       faControl['controls'].bsku.setValue(sku);
      }
    

    Hope this helps. Happy coding.

    0 讨论(0)
  • 2021-01-01 02:31

    If you're getting the error "ERROR Error: Cannot find control with path: 'addresses -> 0 -> addressType'" or something similar, it's most likely cause you're passing the values in but your html template is expecting values along with a control name.

    To resolve this, iterate over each item in your array, and create a new form group instance for each value - based on what your view is expecting - then push to a new array variable, which we then set in our form.

    See code below:

    var tagsArray = [];
    this.product.tags.forEach(product => tagsArray.push(this.fb.group({tag: [product.tag, [Validators.required]]})));
    this.productForm.setControl('tags', this.fb.array(tagsArray || []));
    

    Referenced in view like so:

    <div class="tag" *ngFor="let t of tags.controls; let i = index" [formGroupName]="i" class="mb-2">
          <input type="text" formControlName="tag" placeholder="Tag name" class="primary_input">
           <button mat-icon-button (click)="deleteTag(i)">
               <mat-icon>clear</mat-icon>
           </button>
     </div>
    

    This lets you load previous results while enabling the ability to add more values and validate user input.

    Hope this helps.

    0 讨论(0)
  • 2021-01-01 02:48

    For arrays, you need to use setControl. Something like this:

        this.productForm = this.fb.group({
            productName: ['', [Validators.required,
                               Validators.minLength(3),
                               Validators.maxLength(50)]],
            productCode: ['', Validators.required],
            starRating: ['', NumberValidators.range(1, 5)],
            tags: this.fb.array([]),
            description: ''
        });
    
        ...
    
        // Update the data on the form
        this.productForm.patchValue({
            productName: this.product.productName,
            productCode: this.product.productCode,
            starRating: this.product.starRating,
            description: this.product.description
        });
        this.productForm.setControl('tags', this.fb.array(this.product.tags || []));
    
    0 讨论(0)
提交回复
热议问题