In my code, I am filling $scope.master
with data from a csv file. When I populate $scope.master
within the function, all the data is present. This can be observed in the log output displayed below.
However, outside of the function, although $scope.master
contains the expected data, when I execute
console.log(Object.getOwnPropertyNames($scope.master))
, it returns an empty array, and console.log(Object.getOwnPropertyNames($scope.master).length)
also returns 0.
Why does the seemingly non-empty $scope.master
object show an empty array for its properties?
angular.module('app', []);
angular.module('app').controller('mainCntrl', ['$scope',
function ($scope) {
$scope.master = {};
$scope.selected_interface = "";
$scope.selected_DBCfile = "";
$scope.DBCfiles = [];
var CSV_filepath = '../data/interfaces.csv';
d3.csv(CSV_filepath, function (err, data) {
data.forEach(function (d) {
d.interfaceName = d.interfaceName;
d.DBCfile = d.DBCfile;
d.AtoB = +d.AtoB;
d.BtoA = +d.BtoA;
if (!$scope.master[d.interfaceName]) {
var DBCfilesForInterface = {};
DBCfilesForInterface[d.DBCfile] = new Array();
DBCfilesForInterface[d.DBCfile].push(d);
$scope.master[d.interfaceName] = DBCfilesForInterface;
}
else if (!$scope.master[d.interfaceName][d.DBCfile]) {
$scope.master[d.interfaceName][d.DBCfile] = new Array();
$scope.master[d.interfaceName][d.DBCfile].push(d);
}
else{
$scope.master[d.interfaceName][d.DBCfile].push(d);
}
})
//master is all made
$scope.interfaces = Object.keys($scope.master);
$scope.selected_interface = $scope.interfaces[0];
$scope.DBCfiles = Object.keys($scope.master[$scope.selected_interface]);
$scope.selected_DBCfile = $scope.DBCfiles[0];
//LOOK AT THESE LOGS
console.log($scope.master);
//Object { 1 - ModelS ESP 1.0 Interface: Object, 2 - ModelS ESP 2.0 Interface: Object, 3 - ModelS ESP 2.0 Interface with Tesla Body Controls: Object, 4 - ModelS ESP 2.0 Interface with Tesla Body Controls and TH bus: Object }
console.log($scope.selected_interface);
//"1ModelSESP10Interface"
console.log($scope.selected_DBCfile);
//"ModelSBDYdbc"
console.log($scope.DBCfiles);
//Array [ "ModelSBDYdbc", "ModelSBFTdbc", "ModelSCHdbc", "ModelSETHdbc", "ModelSOBDIIdbc", "ModelSPTdbc", "ModelXTHcommondbc" ]
});
//OUTSIDE OF THE FUNCTION
console.log($scope.master);
console.log(Object.getOwnPropertyNames($scope.master));
console.log(Object.getOwnPropertyNames($scope.master).length);
console.log($scope.selected_interface);
console.log($scope.selected_DBCfile);
console.log($scope.DBCfiles);
}]);