Angular Material - show mat-error on button click

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

    I am providing 3 different solutions for different scenarios, use the one which suits you.

    • If you are using a form, then do

      this.form.markAllAsTouched();
      
    • If you need a particular field to be affected inside form, then filter that nameControl and do

      nameControl.markAsTouched();
      
    • If you are not using forms, then specify a ref for the input element and initialize variable in ts file & do as follows,

      @ViewChild('myInputRef') myInputRef; // Initialize the ref of input element
      .
      .
      this.myInputRef.control.markAsTouched()
      

提交回复
热议问题