I have an Angular (Angular 9) parent component that holds a form. I want to abstract and generalize display of validation errors, so I have created a child component to handle validation error display. Specifically, I'm trying to assign a string to an ngIf to display an error message if one exists dynamically. Here is my template:
<div class="alert">
<div *ngFor="let i of InnerDisplayData">
<div *ngIf="'i[0]'">
{{ i[1] }} <br />
{{ i[0] }}
</div>
</div>
</div>
Here is an excerpt from my component file:
this.InnerDisplayData = Array.from(this.valMap, ([x, y]) => [
"(fg | async).controls['" + this.field + "'].errors." + x,
y,
]);
const sub: Subscription = this.fg.subscribe((x) => {
console.log(JSON.stringify(x.controls["FirstName"].errors));
});
Where fg is @Input() fg: Observable<FormGroup>
fg
is triggering correctly, and the 'required' validator is popping true when it should for the control. My template shows that the string being generated (i[0]
) for the innerDataDisplay is this:
(fg | async).controls['FirstName'].errors.required
However, it appears that the ngIf is triggering because it is looking at a truthy string, rather than evaluating the string. Therefore, it is not reacting based on the conditions in the FormGroup and always displays the content. What am I doing wrong? Thanks!