Angularjs,从对象和错误创建数组:$ rootScope:infdig

时间:2016-04-07 15:15:58

标签: angularjs

我的目标是从对象创建一个数组(作为表行),但我得到$ rootScope:infdig错误。

这是我的代码:

$scope.rows = {
"TableName": "TestTable",
"Rows": [{
    "Name": "Name1",
    "RowCells": [{
        "Y": 2001,
        "M": 164
    }, {
        "Y": 2002,
        "M": 178
    }, {
        "Y": 2003,
        "M": 188
    }, {
        "Y": 2007,
        "M": 295
    }, {
        "Y": 2008,
        "M": 316
    }, {
        "Y": 2009,
        "M": 328
    }]
}],
"Rows": [{
    "Name": "Name2",
    "RowCells": [{
        "Y": 2001,
        "M": 164
    }, {
        "Y": 2002,
        "M": 178
    }, {
        "Y": 2003,
        "M": 188
    }, {
        "Y": 2007,
        "M": 295
    }, {
        "Y": 2008,
        "M": 316
    }, {
        "Y": 2009,
        "M": 328
    }]
}],
"Rows": [{
    "Name": "Name3",
    "RowCells": [{
        "Y": 2001,
        "M": 164
    }, {
        "Y": 2002,
        "M": 178
    }, {
        "Y": 2003,
        "M": 188
    }, {
        "Y": 2007,
        "M": 295
    }, {
        "Y": 2008,
        "M": 316
    }, {
        "Y": 2009,
        "M": 328
    }]
}]
}

 $scope.rowCells = function () {
                var tableRowCollection = [];

                angular.forEach($scope.rows, function (item) {
                    var tableRow = [];
                    tableRow.push(item.Name);
                    angular.forEach(item.RowCells, function (i) {
                        tableRow.push(i.M);
                    });
                    tableRowCollection.push(tableRow);
                });
                return tableRowCollection ;
            };

 <div data-ng-repeat="r in rowCells()">{{r}}</div>

我见过类似的问题here,但这并没有帮助。 我错过了什么或我哪里出错了?

2 个答案:

答案 0 :(得分:0)

你的行数组应该是这样的:

$scope.rows = {
"TableName": "TestTable",
"Rows": [{
    "Name": "Name1",
    "RowCells": [{
        "Y": 2001,
        "M": 164
    }, {
        "Y": 2002,
        "M": 178
    }, {
        "Y": 2003,
        "M": 188
    }, {
        "Y": 2007,
        "M": 295
    }, {
        "Y": 2008,
        "M": 316
    }, {
        "Y": 2009,
        "M": 328
    }]
}, {
    "Name": "Name2",
    "RowCells": [{
        "Y": 2001,
        "M": 164
    }, {
        "Y": 2002,
        "M": 178
    }, {
        "Y": 2003,
        "M": 188
    }, {
        "Y": 2007,
        "M": 295
    }, {
        "Y": 2008,
        "M": 316
    }, {
        "Y": 2009,
        "M": 328
    }]
}, {
    "Name": "Name3",
    "RowCells": [{
        "Y": 2001,
        "M": 164
    }, {
        "Y": 2002,
        "M": 178
    }, {
        "Y": 2003,
        "M": 188
    }, {
        "Y": 2007,
        "M": 295
    }, {
        "Y": 2008,
        "M": 316
    }, {
        "Y": 2009,
        "M": 328
    }]
}]
}

答案 1 :(得分:0)

正如Staskus建议的那样,你必须改变阵列。您的foreach循环应如下所示:

angular.forEach($scope.rows.Rows, function(item) {
    var tableRow = [];
    tableRow.push(item.Name);
    angular.forEach(item.RowCells, function(i) {
        tableRow.push(i.M);
    });
    tableRowCollection.push(tableRow);
});