I saw plenty of questions related to TypeError: Cannot read property '0' of undefined but none of them are helpful to solve my error.
My requirement is onClick button(like: ng-click="addRow(tester.devices)" ) I have to call a function-
In which if already devicelist is present/calling then no need to define _scope.tester.devices = []
otherwise _scope.tester.devices = [];
is defined to avoid error: Cannot read property 'unshift' of undefined
My code as follows
_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: I defined $scope as _scope. So this is not problem.
This question solved
Pass array to the function, seems like you are passing wrong parameter to the function _scope.addRow()
it worked fine for me after passing array to the function.
I solved this by modifying _scope.modifyField[list[0].uniqueId] = true;
to _scope.modifyField[_scope.uniqueId] = true;
.
Unfortunately, i unable to delete my question as someone invested time on this as per Stack Overflow rules.
The code as follows:
_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,
});
_scope.modifyField[list[0].uniqueId] = true;
} else {
_scope.tester.devices = [];
_scope.tester.devices.unshift({
'name': '',
'id': '',
'uniqueId': _scope.uniqueId,
});
}
_scope.modifyField[_scope.uniqueId] = true;
}
Hi, Are you sure your function argument ('list') is an array? It seems undefined.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.