2

I get an error in getting the formControlName of a FormBuilder Array:

Error: Cannot find control with path: 'elements -> 0 -> name'

<form [formGroup]="targetAttributesForm" (ngSubmit)="save(myForm)">
  <input formControlName="nono" placeholder="First">

  <div formArrayName="elements">
    <div *ngFor="let address of targetAttributesForm.controls.elements.controls; let w=index" class="panel panel-default">
      <div [formGroupName]="w">
        <span>Att {{w + 1}}</span>
        --> {{ address[w] }}
        <label>Attribut name</label><input type="text" formControlName="name">
        <label>Attribut type</label>
      </div>
    </div>
  </div>
</form>

my app.component.ts:

ngOnInit() {
    this.targetAttributesForm = this._fb.group({
       nono : ['a', Validators.required],
       elements : this._fb.array([this.initAttribut]) 
      });
}
initAttribut() {
  return this._fb.group({
        name : ['a', [Validators.required]],
        type : ['b', Validators.required]
    });
}

This is my error:

Error: Cannot find control with path: 'elements -> 0 -> name' Trace de la pile : _throwError@http://localhost:4200/vendor.bundle.js:69949:11 setUpControl@http://localhost:4200/vendor.bundle.js:69857:9 ../../..

2
  • Welcome to StackOverflow. Please read Asking and How do I ask a good question. I corrected your question: I simplified the title (be as concise as you can!); I used blockquotes for all the error messages; I also rewrote your sentences, removing all useless parts ("please help me!", or multiple question/interrogation marks, for example). Commented Sep 25, 2017 at 21:33
  • manu thanks Massimiliano Commented Sep 25, 2017 at 21:42

1 Answer 1

1

I think you have mistyped because forgot to call your initAttribut function:

this._fb.array([this.initAttribut()])
                               ^^^^^^
                        you need to call this function

Otherwise FormBuilder will create array of one FormControl instead of array of one FormGroup.

StackBlitz Example

Sign up to request clarification or add additional context in comments.

5 Comments

Can we use array of FormControl instead of FormGroup inside elements?
@Nitul It's up to you. You can if you want
I tried but not working, may be somthing missing from my side. I tried defining formControlName="w" and removed formGroupName.
@Nitul Can you prepare a small demo with stackblitz?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.