Angular 获取Typescript类中的值之和
我制作了一个视图组件,它通过@Input接收数据。 a如何获得值的总和?我试过使用构造函数和as函数,但似乎不正常 这是我的示例代码 接口和类 接口IChartData{ 日期:字符串; 狗:数目; 猫:数量; 总数:个; } 类ChartData实现IChartData{ constructorpublic date:字符串,公共犬:编号,公共猫:编号,公共总数:编号=0{ this.total=this.dogs+this.cats; } getTotal{ 归还这个。狗+这个。猫; }Angular 获取Typescript类中的值之和,angular,typescript,Angular,Typescript,我制作了一个视图组件,它通过@Input接收数据。 a如何获得值的总和?我试过使用构造函数和as函数,但似乎不正常 这是我的示例代码 接口和类 接口IChartData{ 日期:字符串; 狗:数目; 猫:数量; 总数:个; } 类ChartData实现IChartData{ constructorpublic date:字符串,公共犬:编号,公共猫:编号,公共总数:编号=0{ this.total=this.dogs+this.cats; } getTotal{ 归还这个。狗+这个。猫; } }
} 在myChartData类中需要全局变量。然后在构造函数中,您需要填充它,然后在getTotal函数中从全局返回值
interface IChartData {
date: string;
dogs: number;
cats: number;
total: number;
}
class ChartData implements IChartData {
myChartData: IChartData;
constructor(public date: string, public dogs: number, public cats: number, public total: number=0 ){
// this.myChartData["date"] = date;
// this.myChartData["dogs"] = dogs;
// this.myChartData["cats"] = cats;
// this.myChartData["total"] = total;
this.myChartData = {
date: date,
dogs: dogs,
cats: cats,
total: total
}
}
getTotal() {
this.myChartData["total"] = this.myChartData["dogs"] + this.myChartData["cats"];
return this.myChartData["total"];
}
}
定义它似乎不正确。你的角度分量和你的图表数据有什么关系?这很混乱。您有一个实现IChartData的类,该类还包含一个IChartData实例?当您尝试获取getTotal时,确切的问题是什么?您的代码应该返回该值。可以创建角度服务来获取和设置值。我想我真的不明白你的问题。