I have a question regarding a small issue with an Angular reactive form in a lazily loaded signup module. The code structure is structured as follows:
TS
get email() {
return this.myForm.get('email');
}
// While this code works from within the component, it does not work when referenced from within the template.
HTML
<div class="errors" *ngIf="email.errors?.required && email.touched">Please enter your email address</div>
In the template, the div is never displayed because the expression never evaluates to true, even when errors are present. I have double-checked in the console.
What's even more confusing is that I can easily use the getter in the component.ts
file by writing console.log(this.email.errors)
, and it works perfectly fine there. However, it doesn't seem to work in the template.
To circumvent the issue, my messy solution has been to access the errors directly which surprisingly works:
<div class="errors" *ngIf="myForm.controls.email.errors.required && myForm.controls.email.touched">
Please enter your email address
</div>
// This workaround works! But the expression is convoluted and lengthy.
Any assistance would be greatly appreciated!