Angular 6 Reactive Forms : How to set focus on first invalid input

前端 未结 6 1333
闹比i
闹比i 2020-12-16 18:21

Under my Angular 6 app , i\'m using Reactive Forms .

My purpose is when submitting , i want to set focus on first invalid input when

6条回答
  •  [愿得一人]
    2020-12-16 18:58

    We can set focus on first invalid input simply by just write this code in the submit() of the form.

       if(this.form.invalid)
        {  
          // Got focus to the error field
        let invalidFields = [].slice.call(document.getElementsByClassName('ng-invalid'));
        invalidFields[1].focus();  
    
        }
    

提交回复
热议问题