quick question. It's one of those small annoying bugs. I have an Angular reactive form in a lazy-loaded signup module. The code structure is as follows:
TS
get email() {
return this.myForm.get('email');
}
// This code works from within the component, but not when referenced from within the template.
HTML
<div class="errors" *ngIf="email.errors?.required && email.touched">Must enter email</div>
In the template, the div is never shown since the expression never evaluates to true, even when there are errors present. I've checked in the console.
What's more confusing is that I have no problem using the getter from within the component.ts
, in fact, the way I console.log
it is by writing console.log(this.email.errors)
. And That works fine. But not in the template.
My messy solution is to access the errors directly, and that does work:
<div class="errors" *ngIf="myForm.controls.email.errors.required && myForm.controls.email.touched">
Must enter email
</div>
// This works! As you can see the expression is very long and messy.
Any help appreciated!