尝试推入新的嵌套对象。继续在第3行获取cannot read property push of undefined
。
为什么这不起作用?我应该这样做吗?
$scope.item.deliverables[0].steps[0].versions = [];
$scope.item.deliverables[0].steps[0].versions.push({assets:[{url:'aaa'}]})
$scope.item.deliverables[0].steps[0].versions.assets.push({url:'bbb'})
答案 0 :(得分:5)
你需要像这样的数组访问versions
:
$scope.item.deliverables[0].steps[0].versions = [];
$scope.item.deliverables[0].steps[0].versions.push({assets:[{url:'aaa'}]})
// the item you just pushed in the array is in .versions[0] now
$scope.item.deliverables[0].steps[0].versions[0].assets.push({url:'bbb'})
既然你这样做了:
$scope.item.deliverables[0].steps[0].versions = [];
versions
是一个数组,assets
作为该数组的属性是undefined
。因此,错误
cannot read property push of undefined