YugoAmaryl
YugoAmaryl

Reputation: 403

How do I use if condition when setting the routerLink

<div id="stage">
    <div class="space-item"
       *ngFor="let space of spaces; let i=index; trackBy:trackBySpaces"
       routerLink="/space/{{space.space_id}}"
       [ngStyle]="spaceStyles(positions[i].x, positions[i].y)"
    >
        {{space.space_id}}|{{space.name}}
    </div>
</div>

In the code above, how could I do NOT set the routerLink attribute when the space.space_id is empty, I wish to have something like this.

if(space.space_id){routerLink="/space/{{space.space_id}}"}

but I cann't find any solution. Thanks in advance!

Upvotes: 2

Views: 923

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657366

This can be used in template:

[routerLink]="space.space_id ? '/space/' + space.space_id : 'defaultUrl'"

Upvotes: 1

Related Questions