I am writing a form in Angular 2 where the user submits the form, it is validated and if there are any errors with the inputs, I want to scroll the user's browser to the first element with the class "error"
The problem is, all of my errors use *ngIf like so:
<input type="text" [(ngModel)]="model.first_name">
<div class="error" *ngIf="errors.first_name">
{{errors.first_name}}
</div>
In my submit function
submit(){
this.errors = this.validate();
if(this.errors.any()){
var errorDivs = document.getElementsByClassName("error");
if(errorDivs.length > 0){
errorDivs[0].scrollIntoView();
}
}
}
I realize this is because *ngIf removes the div from the DOM completely and the Angular check for changes hasn't been given a chance to run yet. Is there a clever and clean way to do this?
[hidden]
instead of*ngIf
– DDRamone