Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

let variable in angular ngif with multiple conditions

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?

like image 277
serge Avatar asked Dec 02 '25 17:12

serge


2 Answers

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()

like image 87
Serge Avatar answered Dec 05 '25 06:12

Serge


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>
like image 45
serge Avatar answered Dec 05 '25 08:12

serge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!