简体   繁体   English

如何在angular2中将数组的索引从一个组件传递到另一个组件?

[英]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. 目前,我正在使用angular,并且在这样的打字稿文件中有一个数组。

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

In my html I have code like 在我的HTML中,我有类似的代码

<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. 如何将数组的索引传递给series-prop组件。 I need the index value to assign some values in series-prop component. 我需要索引值才能在series-prop组件中分配一些值。

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

In your component: 在您的组件中:

@Input()
index: Number;

*ngFor exposes the index as a local variable. * ngFor将索引显示为局部变量。

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

You can see the documentation here . 您可以在此处查看文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Angular2中将值从一个组件传递到另一个组件? - How to pass value from one component to another component in Angular2? 如何在Angular2中将变量值从一个组件传递到另一个组件 - How to pass variable value from one component to another in Angular2 如何将数据从一个组件传递到另一个 angular2 - How to Pass the data from one component to another angular2 如何将数据从一个子组件传递到angular2中的另一个子组件? - How to pass data from one child component to another child component in angular2? 将参数从一个组件传递到另一个angular2 - pass params from one component to another angular2 如何将响应(数组)从一个组件传递到另一个组件? - how to pass response (array ) from one component to another in angular? 如何将带有 id 的数组从一个组件传递到另一个组件(角度) - How to pass an array with id's from one component to another (angular) 如何从另一个组件调用一个组件中的方法是 angular2 - how to call a method in one component from another component is angular2 如何在component.ts中将数据从一个函数传递到另一个函数,angular2 - how to pass data from one function to another in component.ts , angular2 如何将对象从一个组件传递到另一个组件以在angular2中建立数据模型? - how to pass an object from one component to another to build a data model in angular2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM