1let form: FormGroup = new FormGroup({});
2form.addControl(field_name, new FormControl('', Validators.required));
1this.myForm = this.fb.group({
2 name: ['', [Validators.required]],
3 contacts: this.fb.group({
4 email: ['', [Validators.email]],
5 })
6});
7
8<form [formGroup]="myForm" (ngSubmit)="onSubmit()">
9<mat-grid-list cols="2" rowHeight="85px">
10<!-- name -->
11<mat-grid-tile>
12 <mat-form-field>
13 <input matInput type="text" formControlName="name">
14 <mat-error *ngIf="form.controls.name.hasError('required')">Name required</mat-error>
15 </mat-form-field>
16</mat-grid-tile>
17<!-- contacts -->
18<div formGroupName="contacts">
19 <!-- email -->
20 <mat-grid-tile>
21 <mat-form-field>
22 <input matInput type="text" formControlName="email">
23 <mat-error *ngIf="form.get('contacts').get('email').hasError('required')">Email required</mat-error>
24 </mat-form-field>
25 </mat-grid-tile>
26</div>
27</form>
28</mat-grid-list>
1
2 content_copy
3
4 const form = new FormGroup({
5 first: new FormControl('Nancy', Validators.minLength(2)),
6 last: new FormControl('Drew'),
7});
8
9console.log(form.value); // {first: 'Nancy', last; 'Drew'}
10console.log(form.status); // 'VALID'
11