【问题标题】:How to pass index of an array from one component to another in angular2?如何在angular2中将数组的索引从一个组件传递到另一个组件?
【发布时间】:2017-03-22 06:23:43
【问题描述】:

目前我正在使用 angular,并且我在 typescript 文件中有一个这样的数组。

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

在我的 html 中,我有类似

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

如何将数组的索引传递给 series-prop 组件。我需要索引值在 series-prop 组件中分配一些值。

【问题讨论】:

  • 你的意思是一个JS对象。

标签: angular typescript


【解决方案1】:
<div *ngFor="let val of channelArray; let i = index">
  <de-series-prop [index]="i"></de-series-prop>
</div>

在您的组件中:

@Input()
index: Number;

【讨论】:

    【解决方案2】:

    *ngFor 将 index 公开为局部变量。

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

    您可以查看文档here。

    【讨论】:

    • 感谢您提供此信息。
    猜你喜欢
    • 1970-01-01
    • 2017-04-30
    • 2018-01-24
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多