Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 计算子对象值并显示在父对象上_Arrays_Angularjs - Fatal编程技术网

Arrays 计算子对象值并显示在父对象上

Arrays 计算子对象值并显示在父对象上,arrays,angularjs,Arrays,Angularjs,假设这是我想在AngularJS视图(荷兰语)中发布的一些数据: 要在视图的列表中发布此对象,我可以执行以下操作: <ul ng-repeat="item in data"> <li>{{item.name}}</li> <ul ng-repeat="child in item.children"> <li>{{ child.name }}</li> <ul ng-rep

假设这是我想在AngularJS视图(荷兰语)中发布的一些数据:

要在视图的列表中发布此对象,我可以执行以下操作:

<ul ng-repeat="item in data">
    <li>{{item.name}}</li>
    <ul ng-repeat="child in item.children">
        <li>{{ child.name }}</li>
        <ul ng-repeat="value in child.value">
            <li>{{ value }}</li>
        </ul>
    </ul>
</ul>
var myArray = [];
myArray[0] += 1; // error: there is no element at the 0 position!
我的父值数组中有4个位置,但所有位置都是
NaN
,我做错了什么

次要问题

因为我在我的视图中做循环,难道没有一个更短的方法来计算数组位置的总数吗

这种语法

$scope.data[i].value[k] += ... // some value
假设数组的
k
位置中已有一个要递增的值

在代码中,数组最初是空的。发生的事情是这样的:

<ul ng-repeat="item in data">
    <li>{{item.name}}</li>
    <ul ng-repeat="child in item.children">
        <li>{{ child.name }}</li>
        <ul ng-repeat="value in child.value">
            <li>{{ value }}</li>
        </ul>
    </ul>
</ul>
var myArray = [];
myArray[0] += 1; // error: there is no element at the 0 position!
解决此问题的一种方法是使用零值初始化空数组:

$scope.data[i].value = [0, 0, 0, 0]; // you can also seed the initial zeroes using a loop

你确定吗?因为当我做
var myArray=[];myArray+=5
我刚得到
5
。但是当我执行
var myArray=[]时;myArray+=5;myArray+=10
我得到
510
。我看到的是它不是计算数字,而是计算它们。根据我对你问题的理解,你要做的是在数组中增加一个值,它的语法是
myArray[index]+=value
。因此,如果您有
var myArray=[1,2,3]
并且希望将
5
添加到第一个元素,那么您将执行
myArray[0]+=5
。注意索引
0
的使用。没有索引(
myArray+=5
)的语法不是您在本例中要查找的语法。您是对的,我一直在等待,直到我可以编辑以前的答案,因为我注意到了这一点!我们会成功的,非常感谢!