簡體   English   中英

如何在angular2中將數組的索引從一個組件傳遞到另一個組件?

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

目前,我正在使用angular,並且在這樣的打字稿文件中有一個數組。

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

在我的HTML中,我有類似的代碼

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

如何將數組的索引傳遞給series-prop組件。 我需要索引值才能在series-prop組件中分配一些值。

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

在您的組件中:

@Input()
index: Number;

* ngFor將索引顯示為局部變量。

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

您可以在此處查看文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM