如何在打字稿中访问字段验证的错误?

时间:2017-06-28 07:00:34

标签: javascript angular typescript

我在一个字段上进行了验证:

<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>

如何在打字稿方面访问street.errors

提前致谢。

1 个答案:

答案 0 :(得分:2)

您可以使用@ViewChild

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

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

        private checkElementStatus(): void { 
           console.log(this.streetElement.errors) 
        }
}