showing results for - "angular material mat error on http request"
Miguel Ángel
29 Oct 2020
1// first define our formcontrol object
2control = new FormControl('', [Validators.required])
3
4//http method:
5submit() {
6  //call api
7  // assume you got an error (Unauthorized, BadRequest, ...)
8  //this set control to errored status and thus,
9  //mark formcontrol/formgroup as invalid
10  this.control.setErrors({error_key: true})
11}
12
13// in your html code :
14<mat-form-field>
15  <mat-label>Control</mat-label>
16  <input type="text" formControlName="control" matInput />
17  <mat-error *ngIf="control.hasError('required')">
18  	Control cannot be empty
19  </mat-error>
20  <mat-error *ngIf="control.hasError('error_key')">
21  	My custom error message
22  </mat-error>
23</mat-form-field>