我有以下代码:
if (!$scope.aa.hasOwnProperty('x')) {
$scope.aa.x = {}
data.answers.forEach(function (element, index) {
$scope.aa.x[index].c = null;
$scope.aa.x[index].r = null;
$scope.aa.x[index].text = element.text;
});
}
但这给了我一个错误:
TypeError: Cannot set property 'c' of undefined
我需要为它定义一个数组aa
吗,如果要怎么做呢?
是的。看起来就像x
是array
和x[index]
是object
:
$scope.aa.x = [];
data.answers.forEach(function (element, index) {
$scope.aa.x[index] = {};
$scope.aa.x[index].c = null;
$scope.aa.x[index].r = null;
$scope.aa.x[index].text = element.text;
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句