Search code examples
javascriptangularnativescriptangular2-nativescript

conditional set angular/nativescript nsRouterLink


Is it possible to set a nsRouterLink only when a specific condition is true?

Let's assume, I have a list view and each item gets its own nsRouterLink to a detail page. But some items in the list do not have the id needed for the detail, so they should simply not be clickable while the others should.

I know that I could just use an onClick binding and handle it on my own, but is that possible with nsRouterLink automatically, too?

For example:

<StackLayout *ngFor="let item of items">
    <StackLayout [nsRouterLink]="['/path/' + item.id]">
        <!-- Content goes here -->
    </StackLayout>
</StackLayout>

Solution

  • What is with this solution I saw several times:

    <StackLayout *ngFor="let item of items">
        <StackLayout [nsRouterLink]="[item?.id ? '/path/' + item.id : false]">
            <!-- Content goes here -->
        </StackLayout>
    </StackLayout>