import { FormGroup, FormBuilder } from '@angular/forms';
form: FormGroup;
model: OrganizationAddModel;
constructor(private formBuilder: FormBuilder, private organizationService: OrganizationService) {
this.model = new OrganizationAddModel();
}
ngOnInit() {
this.form = this.formBuilder.group({
name:[],
email:[]
});
}
<form [formGroup]="form">
<input [(ngModel)]="model.name" formControlName="name" matInput placeholder="Name">
<div *ngIf="form.controls.name.invalid">
{{form.controls.name.errors.message}}
</div>
<input [(ngModel)]="model.email" formControlName="email" matInput placeholder="Email">
</form>
let controls = this.form.controls;
for (var field in controls) {
controls[field].setErrors({
message: "This is required field"
});
}