Angular Material - show mat-error on button click

后端 未结 10 1792
刺人心
刺人心 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:12

    the easiest way is call markUserNameTouched() method as below on button click on template. We use markAsTouched() on formControl.

    public staffLoginForm: FormGroup;
    ngOnInit(){
    this.staffLoginForm = new FormGroup({
    username: new FormControl(null),
    password: new FormControl(null)});
    
    markUserNameTouched():void{
      this.staffLoginForm.get('username').markAsTouched();
    }
    

提交回复
热议问题