无法在角度js中设置对象的属性

时间:2016-05-09 11:35:27

标签: javascript angularjs

$scope.objectData = {};
$scope.objectData[key]["digits"] =  set.first+','+set.second+','+set.third+','+set.fourth;

此处键是一个数值。错误是

TypeError: Cannot set property 'digits' of undefined

2 个答案:

答案 0 :(得分:1)

您需要先为对象设置$scope.objectData[key]的值,然后才能为其添加更多键。

$scope.objectData[key] = {};
$scope.objectData[key]['digits'] = 'foo';

答案 1 :(得分:0)

首先必须初始化对象$scope.objectData[key],以便正确的代码为:

$scope.objectData = {};
$scope.objectData[key] = {};
$scope.objectData[key]["digits"] = set.first+','+set.second+','+set.third+','+set.fourth;