I have a material form in which I have an input box:
<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});
I get the following error while loading:
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)
You can create the methods in your
component
to check state and validation a field inFormGroup
:Then use this method as clause on
*ngIf
:Because you have to get the control from the form like this :
Or