5

I am working on Angular Reactive forms, I have added a couple of FormControl into FormArray dynamically, but I am getting issue to reference those FormControls in my template while binding them with formControlName. In template I assigned loop index variable "i" to formControlName but its not working. I get "No value accessor for form control with path" by binding controls with formControlName. here is my component class code:

export class Control {
  constructor(public controlType?: string, public label?: string, public required?: boolean, public placeholder?: string,
    public options?: string[], public value?: string) { }
}

export class FormComponent implements OnInit {
  reviewForm: FormGroup;

  constructor(public formService: FormService, public dialog: MdDialog) { }


  selectedControl: any = null;
  label: string;

  ngOnInit() {
    this.reviewForm = new FormGroup({
      'controlArray': new FormArray([
      ])
    });
  }

  addControl() {
    const myControl: Control = new Control(this.selectedControl.name, this.label, this.isRequired,
      this.selectedControl.attributes.value, this.selectedControl.attributes.options, 'null');
    var control: FormControl = new FormControl(myControl);
    (<FormArray>this.reviewForm.get('controlArray')).push(control);
  }

}

And it is my component's template:

<form [formGroup]="reviewForm" (ngSubmit)="onSubmit()">
  <div class="example-label">
    <span class='block'>    
              <span *ngIf="selectedForm">
                      <h2 class="example-heading">{{displayForm}} </h2>
              </span>
    <div formArrayName="controlArray">
      <div *ngFor="let control of reviewForm.get('controlArray').controls; let i = index">
        <table>
          <tr>
            <span *ngIf="control.value">
                    <td> 
                      <label>{{control.value.label}}</label>                           
            </td>
            <td><span *ngIf="control.value.controlType == 'select'">                
                      <md-select placeholder="{{control.value.label}}" [formControlName]="i" >
                        <md-option *ngFor="let option of control.value.options; let i=index" 
                        [value]="option">{{control.value.options[i]}}</md-option>                  
                      </md-select>
                    </span></td>
            <td> <span *ngIf="control.value.controlType == 'text'">
              <md-form-field class="example-full-width">
                <input mdInput type="text" placeholder="{{control.value.placeholder}}" [formControlName]="i" >
              </md-form-field>  
          </span></td>
            <td> <span *ngIf="control.value.controlType == 'radio'">
              <md-radio-group>
                      <span *ngFor="let option of control.value.options">
                        <md-radio-button name="gender" value="{{option}}" [formControlName]="i" >{{option}} </md-radio-button>
                      </span>
              </md-radio-group>
              </span>
            </td>
            <td> <span *ngIf="control.value.controlType == 'checkbox'">
                      <md-checkbox value="{{control.value.controlType}}" [formControlName]="i" > </md-checkbox>
                    </span></td>

            <td> <span *ngIf="control.value.controlType == 'textarea'">
                      <textarea name="Comment" id="" cols="30" rows="10" [formControlName]="i" ></textarea>  
                    </span></td>

            <td> <span *ngIf="control.value.controlType == 'button'">
                        <button md-raised-button value="{{control.value}}" [formControlName]="i" >Button</button>  
                    </span> </td>
            </span>
          </tr>
        </table>
      </div>
    </div>
    </span>
  </div>
</form>

In my component class I only have FormArray in FormGroup. In addControl function I made a custom control and pass this control as a first parameter to a FormControl. Afterwards I push this FormControl into my FormArray. Please help, in template How do I associate formControlName to FormControls. Thanks

14
  • *ngIf="control.value[formControlName]=" i "" what's this syntax?
    – Chau Tran
    Commented Dec 6, 2017 at 15:46
  • Sorry, it was a typo mistake. I have fixed this. @ChauTran Commented Dec 7, 2017 at 6:26
  • 1
    Seems control is an object, therefore you need to use a formGroup instead of a formcontrol.
    – AVJT82
    Commented Dec 10, 2017 at 19:25
  • Would you please show me how? @AJT_82 Commented Dec 11, 2017 at 7:13
  • 1
    Alright, Thanks for help. Commented Dec 11, 2017 at 8:05

3 Answers 3

3

You can bind this way:

reviewForm.controls.controlArray.controls[i].controls.DATAYOULIKEBIND.value
3

I had the same problem and solved it with a getter in the component class.

get controlArray(): FormArray {
    return <FormArray> this.reviewForm.get('controlArray');
}

Note the cast to FormArray, otherwise the getter returns AbstractControl and you can't access any of the array properties or functions.

Then in the template you can do something like this.

<div formArrayName="controlArray">
    <div *ngFor="let control of controlArray.controls; let i = index" >
3
  • Similar to that. See the example at angular.io/api/forms/FormArrayName#example
    – rooby
    Commented Nov 24, 2020 at 10:02
  • ...Then using the i variable as the [formControlName] for your control elements.
    – rooby
    Commented Nov 24, 2020 at 10:10
  • 1
    Remember that I answered this question in Dec 2018 when Angular 7 was the latest version. The docs will be up to date for the latest version. Commented Dec 2, 2020 at 10:15
0

You should do something like this. Add [formGroupName]=i to next div where you have added formArray. So that Angular knows the correct traversal. Hope this helps. I had the same issue sometime before and this fix helped me

<div formArrayName="controlArray"> <div [formGroupName]=i *ngFor="let control of reviewForm.get('controlArray').controls; let i = index">

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.