我在打字稿文件中有以下代码:
attributes.ts
export class AttributesPage {
chart: any[];
constructor(
//Assigning values to chartArray
var chartArray = [1,2,3,4,5,6,7,8,9,10];
//Should define the chart variable
this.chart = chartArray
})
}
}
我想将chart
变量分配给我的.html文件中的参数,但无法通过它并在chart-here
标记中使用它。
attributes.html
<ion-content>
<ion-card-header>
//Displays 1,2,3,4,5,6,7,8,9,10
{{this.chartArray}}
//Displays 1,2,3,4,5,6,7,8,9,10
{{this.chart}}
//Undefined
{{chart}}
<ion-card-content>
//When I try to assign the values of the variables to the [readings] parameter in <chart-here>
//"this.chartArray": undefined
//"this.chart": undefined
//"chart": undefined
<chart-here [readings]= "chart"></chart-here>
</ion-card-content>
</ion-card-header>
或者,这可行:
attributes.html
<ion-content>
<ion-card-header>
<ion-card-content>
<chart-here [readings]= [1,2,3,4,5,6,7,8,9,10]></chart-here>
</ion-card-content>
</ion-card-header>
当我在.ts页面中定义图表变量时:
attributes.ts
export class AttributesPage {
chart: any[] = [1,2,3,4,5,6,7,8,9,10];
constructor()
}
如何将图表变量或通过this.chartArray / this.chart作为变量定义到chart-here
标记?
任何输入或帮助将不胜感激。
答案 0 :(得分:0)
尝试将构造函数中的代码放到ngOnInit方法中,如下所示:
ngOnInit() {
this.chart = [1,2,3,4,5,6,7,8,9,10];
}