I'm trying to render DOM elements based on a condition using directive ngIf
. The condition is determined in a for loop using ngFor
.
To explain further, in the template I have the following:
<div class="flex-card" *ngFor="let thing of things; trackBy: trackByFn">
<div class="card" *ngIf="displayCta">
...
</div>
</div>
In my component I have some corresponding property which is initialised to false:
displayCta = false;
and the corresponding function used by trackBy in the same component is as:
trackByFn(index: any, item: any) {
this.displayCta = (index % 3 === 0);
}
What I would expect is that the element with class card
would be rendered when displayCta is set to true. Debugging this does show that displayCta toggles between true and false.
However what's rendered is purely based on the initial state of displayCta, not the logic that modifies it's state.
Is this down to the lifecycle of the component i.e. it looks at the initial state of displayCta, decides whether it's a truthy/falsey does all the rendering and after that the function trackByFn
is invoked making no difference to what's rendered?