GuillaumeC
GuillaumeC

Reputation: 555

Angular2: Object key as a variable in a template

In my template, I would like to pass an object parameter with a dynamic key.

<li *ngFor="let item of items | keys">
    <a [routerLink]="[url, {objectKey: item.key}]">
        {{ item.key }}{{ item.value }}
    </a>
</li>

In this example, the objectKey is a property of my object, but because it's javascript, it takes it as a string 'objectKey' ...

Upvotes: 2

Views: 2991

Answers (2)

Vamsi
Vamsi

Reputation: 9780

<li *ngFor="let item of items | keys">
    <a [routerLink]="['/url', item.key]">   //<-- arguments should be array of
                                                   elements that form your url
        {{ item.key }}{{ item.value }}
    </a>
</li>

Upvotes: 3

George Kagan
George Kagan

Reputation: 6124

Use square brackets (if EcmaScript 6 is used):

{[objectKey]: item.key}

Upvotes: 2

Related Questions