In Angular (13) is there a way to assign the result of a function to a variable (in the .html part of a component, not template) having multiple conditions in ngIf
<div *ngIf="let getMyVar() as myVar && isVisible && isClean">
{{ 'this is myVar: ' + myVar }}
</div>
if not what workaround is possible to implement?
The conditionals seem to use standard Javascript operator logic:
Mozilla documentation for the Logical AND operator states the following:
...the operator returns the value of the first falsy operand encountered when evaluating from left to right, or the value of the last operand if they are all truthy.
As such, the following works:
<div *ngIf="isVisible && isClean && getMyVar(); let myVar">
{{ 'this is myVar: ' + myVar }}
</div>
I tested this locally, and it was successful at displaying the value of getMyVar()
did not find anything better than splitting the ngIf in two
<ng-container *ngIf="getMyVar(); let myVar">
<div *ngIf="isVisible && isClean">
{{ 'this is myVar: ' + myVar }}
</div>
</ng-container>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With