我看到了很多相关的问题 类型错误:无法读取未定义的属性“0”,但它们都无助于解决我的错误。
我的要求是 onClick 按钮(例如:ng-click="addRow(tester.devices)")我必须调用一个函数 -
如果设备列表已经存在/正在调用,则无需定义_scope.tester.devices = []
,否则_scope.tester.devices = [];
是定义以避免错误:
无法读取未定义的属性“unshift”
我的代码如下
_scope.addRow = function (list) {
_scope.uniqueId = Math.round((Math.random() * 10) * 10000);
if (_scope.tester.devices) {
_scope.tester.devices.unshift({
'name': '',
'id': '',
'uniqueId': _scope.uniqueId,
});
} else {
_scope.tester.devices = [];
_scope.tester.devices.unshift({
'name': '',
'id': '',
'uniqueId': _scope.uniqueId,
});
}
_scope.modifyField[list[0].uniqueId] = true;
}
Edit1:我将 $scope 定义为 _scope。所以这不是问题。
这个问题已解决
最佳答案
将数组传递给函数,似乎您向函数_scope.addRow()传递了错误的参数
将数组传递给函数后,它对我来说效果很好。
关于javascript - 类型错误 : Cannot read property '0' of undefined in Angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55278787/