基于价值Angularjs NG-重复运行总计价值、Angularjs、NG

2023-09-13 05:21:23 作者:归来仍少年

在angularjs工作,并寻找到正在运行的总保持基于从检索到的数据值。下面code始终返回所有项目的全部总价值。

 < D​​IV NG重复=孩子pItem.childItems |排序依据:'displayOrder'NG-的init =$ parent.totalValue = $ parent.totalValue + child.field [0]。价值> 

改变从父范围,孩子清除总价值上的每个重复。因此,值总是等于孩子的价值只是一个项目。

我在找东西返回类似于以下。凡项目1有一个值2,项目2的值3,和项目3值4。

项目1 2 项目2 5 项目3 9 解决方案

尝试这样的事情,这应该做的伎俩:

 < D​​IV  NG-重复=孩子pItem.childItems |排序依据:'displayOrder'  NG-的init =child.totalValue = pItem.childItems [$指数1] .totalValue + child.field [0] .value的>    {{child.field [0] .value的}} {{child.totalValue}}< / DIV> 

所以你基本上是用previous totalvalue的结果总结。

Working in angularjs and looking to keep a running total based on a value retrieved from the data. The following code always returns the full total value for all items.

<div ng-repeat="child in pItem.childItems | orderBy:'displayOrder'" ng-init="$parent.totalValue = $parent.totalValue + child.field[0].value">

Changing that from the parent scope to the child clears the totalValue on each repeat. So the value always equals the child value just for that one item.

I'm looking for something which returns similar to the following. Where Item1 has a value 2, Item2 a value 3, and Item3 a value 4.

Item1 2 Item2 5 Item3 9

解决方案

Try something like this, this should do the trick:

<div 
  ng-repeat="child in pItem.childItems | orderBy:'displayOrder'" 
  ng-init="child.totalValue = pItem.childItems[$index-1].totalValue + child.field[0].value">
    {{child.field[0].value}} {{child.totalValue}}
</div>

So you basically sum with the previous totalValue result.