Angular Material - show mat-error on button click

后端 未结 10 1797
刺人心
刺人心 2021-02-02 07:48

I am trying to do validation using the and . This works fine when user tabs out of the input without filling. B

10条回答
  •  既然无缘
    2021-02-02 08:29

    Since you want to show mat error on button's click, please try the below: For Angular6 version:

    1). import below:
        import { FormControl, FormBuilder, FormGroup } from '@angular/forms';
    
    2). declare form control in .ts file:
        nameControl = new FormControl('');
    
    3). put control in html:
        
          
          name is required
        
    
    3). on button's click:
        this.nameControl.markAsTouched();
    

    It's important to check how you are using the form control, ".markAsTouched()" on point 3 will show the mat error for the corresponding form control.

提交回复
热议问题