I can't access the JavaScript object's property even though the property is there. I am trying to access the Factory object's property in AngularJS. Below is the code:
angular.module("appManager")
// factories.js
.factory("DataFactory", function ($http) {
var fac = {};
fac.expenses = {};
$http.get("mvc/m/revenue.json")
.success(function (result) {
console.log(result); // line 9
fac.expenses = result.expenses;
});
console.log(fac); // line 13
return fac;
})
// expenseController.js
.controller("expenseCtrl", function($scope, DataFactory){
$scope.data = DataFactory.expenses;
console.log(DataFactory); // line 4
console.log(DataFactory.expenses); // line 5
console.log($scope.data); // line 6
// Global // var d = {};
d = DataFactory;
e = DataFactory.expenses;
});
And the ouput in the console:

Someone also asked (here) about this but he did not get a solution. I also tried what was suggested there: using keys, the console throws "undefined function" error. You can also notice that the log() // 9 inside the log inside $http is called later than other function; can this be the reason? But notice the log() // 13 can already print the object.
I thought there may be something wrong with AngularJS, and I tried some global variables d and e, for testing purpose, which also provide the same result.
I also tried subscripts: DataFactory["expenses"] with same result
Can someone please tell me what I am doing wrong?
d.expenseshas data butedoesn't, that means that somewhered.expenses(orDataFactory.expenses) is replaced with another object, so it becomes different thane.fac.expensesis not valid outside of the.successhandler where you set it. The other places you're trying to look at it, it likely has not yet been set.