Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access field validation's errors in typescript?

I have this validation on a field:

<div class="col-lg-6">
          STRADA
          <input name="street" #street="ngModel" class="form-control" [(ngModel)]="model.Address.Street" required/>
          <div *ngIf="street.errors && (street.dirty || street.touched)" class="alert alert-danger">
             <div [hidden]="!street.errors.required">
                Strada este obligatorie!
             </div>
          </div>
 </div>

How can I access street.errors in the typescript side ?

Thanks in advance.

like image 646
Mihai Alexandru-Ionut Avatar asked Feb 22 '26 16:02

Mihai Alexandru-Ionut


1 Answers

You can access the Element by using @ViewChild

 import { ....., ViewChild } from '@angular/core';

    export class YourComponent.... { 
       @ViewChild('street') streetElement;

        private checkElementStatus(): void { 
           console.log(this.streetElement.errors) 
        }
}
like image 94
Avisek Chakraborty Avatar answered Feb 24 '26 05:02

Avisek Chakraborty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!