0
Error:
ngModel cannot be used to Register form Controls with a Parent formGroup Directive. Try using formGroup’s Partner Directive "formControlName" Instead. Example:
My HTML
<div class="container">
<form [formGroup]="createForm" (ngSubmit)="postCreateFields()" style="width:400px; margin: 0 auto;">
<h1>Create Fields</h1>
<div class="required-field-block">
<input formControlName="name" type="text" placeholder="Nome" class="form-control">
<div class="required-icon">
<div class="text">*</div>
</div>
</div>
<div class="required-field-block">
<input formControlName="typeField" type="text" placeholder="Field" class="form-control">
<div class="required-icon">
<div class="text">*</div>
</div>
</div>
<div>
<mat-form-field>
<mat-select formControlName="typeField" placeholder="Type Fields" [(ngModel)]="dataSource" name="field">
<mat-option *ngFor="let field of dataSource" [value]="field.name">
{{field.name}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
<button type="submit" class="btn btn-primary" >Criar</button>
</form>
</div>
My Component
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '../../../node_modules/@angular/forms';
import { CreateFieldsService } from '../Services/Fields/create-fields.service';
@Component({
selector: 'app-create-fields',
templateUrl: './create-fields.component.html',
styleUrls: ['./create-fields.component.css'],
providers: [CreateFieldsService]
})
export class CreateFieldsComponent implements OnInit {
createForm :FormGroup;
private _createFields: Model.CreateFields;
selectedValue: string;
private operUrl: 'api/TypesFields';
public dataSource: Model.Itens[];
constructor(private _createFieldService: CreateFieldsService, private builder: FormBuilder) {
this.createForm = this.builder.group({
name: ''
})
}
ngOnInit() {
this._createFieldService
.getAll<Model.Result>()
.subscribe((data: Model.Result) => {
debugger;
this.dataSource = data.itens;
});
}
onPostCreateFields(){
this._createFields = this.createForm.value;
this._createFieldService.postCreateFields(this._createFields)
.subscribe( success => {
if(success.Result){
}
},
error =>{
}
);
}
}
How do I fix this error? The error in the browser points to line 13 of the html, referring to this:
<input formControlName="typeField" type="text" placeholder="Field" class="form-control">
EDIT1
Actually the problem is in my mat-select, in this html line
<div>
<mat-form-field>
<mat-select formControlName="typeField" placeholder="Type Fields" [(ngModel)]="dataSource" name="field">
<mat-option *ngFor="let field of dataSource" [value]="field.name">
{{field.name}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
EDIT2
I changed it to this and no longer gave the error
<div>
<mat-form-field>
<mat-select formControlName="typeField" placeholder="Type Fields" [(ngModel)]="typeField" name="field">
<mat-option *ngFor="let field of dataSource" [value]="field.name">
{{field.name}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
EDIT3
so became my compoent
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '../../../node_modules/@angular/forms';
import { CreateFieldsService } from '../Services/Fields/create-fields.service';
@Component({
selector: 'app-create-fields',
templateUrl: './create-fields.component.html',
styleUrls: ['./create-fields.component.css'],
providers: [CreateFieldsService]
})
export class CreateFieldsComponent implements OnInit {
createForm :FormGroup;
private _createFields: Model.CreateFields;
selectedValue: string;
private operUrl: 'api/Fields';
public dataSource: Model.CreateTypeFieldItem[];
constructor(private _createFieldService: CreateFieldsService, private builder: FormBuilder) {
this.createForm = this.builder.group({
name: '',
typeField: ''
});
}
ngOnInit() {
this._createFieldService
.getAll<Model.Result<Model.CreateTypeFieldItem>>()
.subscribe((data: Model.Result<Model.CreateTypeFieldItem>) => {
debugger;
this.dataSource = data.itens;
});
}
onPostCreateFields(){
let formValue = this.createForm.value;
this._createFields ={
name: formValue.name,
typeField: {
typeFieldId: <string>formValue.typeField
}
};
this._createFieldService.postCreateFields(this._createFields)
.subscribe( success => {
if(success.Result){
}
},
error =>{
}
);
}
}
you cannot use the attribute
name
. You have to useformControlName
. It is well described here: fix(Forms): improve ngModel error messages– William John Adam Trindade
@Williamjohnadamtrindade, I use formControlName now. Check out my post
– pnet
Improve the title of your question. And no.. is not:
<mat-select placeholder="Type Fields" [(ngModel)]="dataSource" name="field">
– William John Adam Trindade
You’re right. I edited it and put it there and it keeps going wrong:
Error trying to diff '_string'. Only arrays and iterables are allowed
. The _string is the field value within the dropdown that I want to persist with.– pnet