我有以下数据:
{
"fruits": [
{
"name": "apple",
"prices": [
{
"2015": 2,
"2014": 3,
"2013": 1
}
]
},
{
"name": "banana",
"prices": [
{
"2015": 4,
"2014": 1,
"2013": 3
}
]
}
]
}在控制器内的.js文件中,我想计算最近一年的价格与前一年的价格之间的差额。
例如:
苹果=3-2= -1
所以在我的HTML中,我可以这样做:
<div ng-repeat="fruit in fruits">
{{fruit.name}}, {{fruit.change}}
</div>我尝试过以下几种方法,但都没有成功:
angular.forEach($scope.fruits, function(fruit, key) {
angular.forEach(fruit.prices, function(price, key) {
this.change = price[0] - price[1];
});
});发布于 2016-01-08 23:39:17
只是一个打字错误,而不是this,使用fruit就可以了。
function calculate(targetYear){
angular.forEach($scope.fruits, function(fruit, key) {
$scope.fruits[key].change = fruit.prices[0][targetYear] - fruit.prices[0][targetYear-1];
});
}您可以使用此函数,如:calculate(2015),它将为每个fruit.change值设置,以显示2015/2014年之间的变化
https://stackoverflow.com/questions/34680649
复制相似问题