简体   繁体   English

将多个参数传递给Angular 2组件

[英]Passing multiple parameters to an Angular 2 component

I'm trying to create a chart component in Angular 2. 我正在尝试在Angular 2中创建一个图表组件。

So far it receives an array of tuples and displays this data as a line chart. 到目前为止,它接收一组元组并将这些数据显示为折线图。

The parent component uses the chart like this: 父组件使用如下图表:

<line-chart [values]="myFirstLineValues"></line-chart>

Now I want to display to lines in the same chart. 现在我想显示在同一图表中的行。 I could add two input parameter (like values1 and values2 ), but maybe later I want to display n lines. 我可以添加两个输入参数(如values1values2 ),但稍后我想显示n行。

So I was thinking of having an array of arrays as input parameter. 所以我想把一个数组数组作为输入参数。 This would work. 这会奏效。 But when I want to individually style the lines I would need more arrays of properties for the styling as well. 但是当我想单独设计线条时,我还需要更多的样式属性。 This doesn't seem to be a very elegant way. 这似乎不是一种非常优雅的方式。

I was hoping for a solution like this (pseudo syntax I just made up): 我希望有这样的解决方案(我刚刚编写的伪语法):

<line-chart>
    <line [value]="myFirstLineValues"/>
    <line [value]="mySecondLineValues" />
</line-chart>

Later I could add styling for the lines with special properties of each line (or even passing a CSS class in for each line). 稍后我可以为每行的特殊属性添加样式(甚至为每行传递一个CSS类)。

Is this or something similar possible in Angular 2? Angular 2中是否有类似的东西? Or am I looking at it the wrong way? 或者我是以错误的方式看待它?

You could maintain an array of object, where object is {style: string, data: []} , making your input of type: {style: string, data: []}[] . 你可以维护一个对象数组,其中object是{style: string, data: []} ,输入类型为: {style: string, data: []}[]

This gives you the array of arrays approach, and associates a style with each set of data. 这为您提供了数组方法方法,并将样式与每组数据相关联。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM