我有一個材料形式,其中我有一個輸入框:角材料形成的驗證錯誤
<md-form-field class="input-full-width">
<input mdInput class="form-control emp-info-input" type="text" placeholder="Description" formControlName="periodDesc">
<md-error *ngIf="periodDesc.errors.required">This field is required</md-error>
</md-form-field>
Formbuilder:
this.apprperiod = this.fb.group({
'periodDesc' : new FormControl(this.periodDesc, [Validators.required,Validators.maxLength(50)])
}, {validator: CustomValidator.validate});
我收到以下錯誤而載入:
ERROR TypeError: Cannot read property 'hasError' of undefined
at Object.TestComponent._co [as updateDirectives] (test.html:33)
at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:13075)
at checkAndUpdateView (core.es5.js:12255)
at callViewAction (core.es5.js:12620)
at execComponentViewsAction (core.es5.js:12552)
at checkAndUpdateView (core.es5.js:12261)
at callViewAction (core.es5.js:12620)
at execEmbeddedViewsAction (core.es5.js:12578)
at checkAndUpdateView (core.es5.js:12256)
at callViewAction (core.es5.js:12620)
這是因爲fi加載時,eld.errors尚未設置。我用'* ngIf =「!periodDesc.valid」'解決了這個問題 – Swoox