Kvasimodo
Kvasimodo

Reputation: 151

How can I make var in *ngIf?

I have situation:

<test-component *ngIf="(items | somePipe).length"
                [items]="items | somePipe"
></test-component>

How can I make var from (items | somePipe) in order to not repeat the same code

If there was possible to do sth like that:

<test-component *ngIf="((items | somePipe) as pipedItems).length"
                [items]="pipedItems"
></test-component>

Upvotes: 1

Views: 71

Answers (1)

Eli Porush
Eli Porush

Reputation: 1567

simply

<test-component *ngIf="items | somePipe as myItems"
            [items]="myItems"
></test-component>

if you have to check exactly the length so you can wrap it with ng-container

<ng-container *ngIf="items | somePipe as myItems"> 
    <test-component *ngIf="myItems.length"
        [items]="myItems"
 ></test-component>
</ng-container>

Upvotes: 1

Related Questions