So I've got a dynamic table that I need to keep track of several properties of. I'm using angularJS to do this and a 2D array of objects to track the elements of the table.
My code looks like this:
$scope.table = [];
$scope.generateTable = function(){
var table = [[], []];
for (var i = 0; i < $scope.height; i++) {
$scope.table[i] = []
for (var j = 0; j < $scope.width; j++) {
$scope.table[i].push({
display: 'X',
alive: false
});
}
}
}
$scope.changeProp = function(x, y){
$scope.table[x][y].alive = !$scope.table[x][y].alive;
$scope.table[x][y].alive ? $scope.table[x][y].display = 'O' : $scope.table[x][y].display = 'X';
}
But it says 'cannot read property of undefined' whenever I try to run the changeProp() function. The elements exist in the array as they should and display properly on the front end, why can't I change the elements in the function with the syntax arr[][]?
EDIT: Upon further review, it appears the problem lies with width not being passed to changeProp properly. Here is the front end code:
<tr ng-repeat="x in table">
<td ng-repeat="y in x track by $index" align="center" ng-click="changeProp($parent.index, $index)">
{{ y.display }}
</td>
</tr>
generateTable? Are you surewidthandheightare defined and > 0 at that time ?