【问题标题】:How to access field validation's errors in typescript?如何访问打字稿中的字段验证错误?
【发布时间】:2017-12-01 09:55:31
【问题描述】:

我在一个字段上有这个验证:

<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

提前致谢。

【问题讨论】:

  • 使用ViewChild() 获取#street。见here。这不是打字稿的事情,而是有角度的事情。

标签: javascript angular typescript


【解决方案1】:

您可以使用@ViewChild 访问元素

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    相关资源
    最近更新 更多