code1
code1

Reputation: 8999

How to pass index of an array from one component to another in angular2?

Currently I am using angular and I have an array in typescript file like this.

var channelArray = {"channel1", "channel2", "channel3"}

In my html I have code like

<div *ngFor="let val of channelArray">
            <de-series-prop></de-series-prop>
            </div>

How do I pass the index of the array to series-prop component. I need the index value to assign some values in series-prop component.

Upvotes: 0

Views: 4399

Answers (2)

Adnan A.
Adnan A.

Reputation: 1982

<div *ngFor="let val of channelArray; let i = index">
  <de-series-prop [index]="i"></de-series-prop>
</div>

In your component:

@Input()
index: Number;

Upvotes: 1

toskv
toskv

Reputation: 31600

*ngFor exposes the index as a local variable.

<li *ngFor="let item of items; let i = index; trackBy: trackByFn">...</li>

You can see the documentation here.

Upvotes: 0

Related Questions