基于值的Angularjs ng-repeat运行总数 [英] Angularjs ng-repeat running total based on value

查看:21
本文介绍了基于值的Angularjs ng-repeat运行总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 angularjs 中工作并希望根据从数据中检索到的值保持运行总数.以下代码始终返回所有项目的完整总值.

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

将其从父作用域更改为子作用域会清除每次重复时的 totalValue.因此,该值始终等于该项目的子值.

我正在寻找返回类似于以下内容的内容.其中,Item1 的值为 2,Item2 的值为 3,Item3 的值为 4.

  1. 项目 1 2
  2. 项目 2 5
  3. 项目 3 9

解决方案

试试这样的方法,应该可以解决问题:

{{child.field[0].value}} {{child.totalValue}}

所以你基本上和之前的 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.

  1. Item1 2
  2. Item2 5
  3. 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.

这篇关于基于值的Angularjs ng-repeat运行总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆