Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Form "Cannot find control with path"

I try to make a dynamic form (so you can limitless add items to a list), but somehow the content of my list is not getting send because it can't find the control with path:

Cannot find control with path: 'list_items -> list_item'

My component:

@Component({   selector: 'app-list',   templateUrl: './list.component.html',   styleUrls: ['./list.component.css'] }) export class ListComponent implements OnInit {    listForm: FormGroup;    constructor(private nodeService: NodeService, private messageService: MessageService, private fb: FormBuilder,                 private listService: ListService) {      this.listForm = this.fb.group({       title: ['', [Validators.required, Validators.minLength(5)]],       list_items: this.fb.array([         this.initListItem(),         ])     });   }     initListItem() {     return this.fb.group({       list_item: ['']     });   }   initListItemType() {     return this.fb.group({       list_item_type: ['']     });   }    addListItem() {     const control = <FormArray>this.listForm.controls['list_items'];     control.push(this.initListItem());   } 

The Template (list.component.html):

<h2>Add List </h2>  <form [formGroup]="listForm" novalidate (ngSubmit)="addList(listForm)">   <div class="form-group">     <input type="text" class="form-control" formControlName="title" placeholder="Title">   </div>    <div formArrayName="list_items">     <div *ngFor="let list_item of listForm.controls.list_items.controls; let i=index" class="panel panel-default">       {{i + 1}}.) <input type="text" formControlName="list_item" placeholder="List Item" class="form-control">     </div>     <a (click)="addListItem()">Add List Item +</a>    </div>    <button type="submit">Submit</button> </form> 

The title works just fine, but I can't find the error I have with the "formControlName", which is causing the error.

Thanks in advance for any help in this issue.

Update with what I changed list.component.html

<h2>Add List </h2>  <form [formGroup]="listForm" novalidate (ngSubmit)="addList(listForm)">   <div class="form-group">     <input type="text" class="form-control" formControlName="title" placeholder="Title">   </div>    <div formArrayName="list_items">     <div *ngFor="let list_item of listForm.controls.list_items.controls; let i=index" class="panel panel-default">       {{i + 1}}.) <input type="text" formControlName="{{i}}" placeholder="List Item" class="form-control">     </div>     <a (click)="addListItem()">Add List Item +</a>    </div>    <button type="submit">Submit</button> </form> 

And in my component I changed the constructor and my addListItem method:

listForm: FormGroup;    constructor(private nodeService: NodeService, private messageService: MessageService, private fb: FormBuilder,                 private listService: ListService) {      this.listForm = this.fb.group({       title: ['', [Validators.required, Validators.minLength(5)]],       list_items: this.fb.array([           [''],         ])     });   }    addListItem() {     const control = <FormArray>this.listForm.controls['list_items'];     control.push(this.fb.control(['']));     console.log(control)   } 
like image 753
dowu Avatar asked Sep 24 '16 18:09

dowu


People also ask

How do I get the form status of an ngform in angular?

When you imported the FormsModule in your component, Angular automatically created and attached an NgForm directive to the <form> tag in the template (because NgForm has the selector form that matches <form> elements). To get access to the NgForm and the overall form status, declare a template reference variable.

What is ngmodel directive in angular?

The ngModel directive declared in the FormsModule lets you bind controls in your template-driven form to properties in your data model. When you include the directive using the syntax for two-way data binding, [ (ngModel)], Angular can track the value and user interaction of the control and keep the view synced with the model.

What is a two-way data binding in angular?

Template-driven forms use two-way data binding to update the data model in the component as changes are made in the template and vice versa. Angular supports two design approaches for interactive forms.

Why join the angular developer's community?

Join the community of millions of developers who build compelling user interfaces with Angular. The modern web developer's platform This website requires JavaScript.


2 Answers

There should be a formControlName in your HTML form mapped to your component file.

<div *ngFor="let list_item of [0,1,2]; let i=index" class="panel panel-default">   {{i + 1}}.) <input type="text" formControlName="{{i}}" placeholder="List Item" class="form-control"> </div> 
list_items: this.fb.array([     [''], //0 points to this     [''], //1 points to this     [''] //2 points to this ]) 
like image 107
manish.nith Avatar answered Oct 11 '22 11:10

manish.nith


Note that if your FormArray contains other FormGroup controls (which contain other instances of FormControl), you'll need to do this to access the controls inside each FormGroup:

        <div *ngFor="let item of myFormArray.controls; let i=index">             <div formGroupName="{{i}}">                 <input formControlName="myFormGroupSubControl1" />                 <input formControlName="myFormGroupSubControl2" /> 
like image 33
Chris Halcrow Avatar answered Oct 11 '22 10:10

Chris Halcrow