簡體   English   中英

Angular Material表單驗證錯誤消息不會消失

[英]Angular Material forms validation error message won't go away

我有一個物料表單,其中有一個MdInput

<md-form-field class="input-full-width">
    <input mdInput class="form-control" type="text" placeholder="Description" formControlName="periodDesc">
    <md-error *ngIf="fb.get('periodDesc').errors.required">This field is required</md-error>
</md-form-field>

觸摸該字段但未鍵入任何文本時,將顯示驗證消息。但是,即使我嘗試在其中鍵入文本,驗證消息仍然存在:

在此處輸入圖片說明

編輯1:

當我執行此操作時,該問題已解決:

<md-error *ngIf="fb.hasError('required', ['periodDesc'])">This field is required</md-error>

但是,我的問題是為什么這個問題首先出現? 前一種情況有什么問題嗎,因為我正在其他地方無法使用的情況下使用它。 參考: Angular Material表單驗證錯誤

原因是當formControl沒有驗證程序錯誤(請參見此處 )時, fb.get('periodDesc').errors將返回null 因此,您當前的方式將引發null error例如can not find required of null

請使用fb.get('periodDesc').hasError('required')代替fb.get('periodDesc').errors.required以防止發生上述錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM