I'm attempting to assign a variable to one found in a controller from a promise taken from a factory, which pulls JSON data from a URL.
webApp = angular.module("App", []);
webApp.factory("getData", function($http) {
var promise;
var getData = {
async: function() {
if ( !promise ) {
promise = $http.get(window.location+"/json").then(function (response) {
return response.data;
});
}
return promise;
}
};
return getData;
});
webApp.controller("View", function(getData, $scope) {
$scope.m = {}
getData.async().then(function(m) {
$scope.m = m;
if ($scope.m == m) {
console.log(true);
}
});
console.log($scope.m);
});
Within the .then function, true is returned - but $scope.m is always returned as undefined.
Any help would be greatly appreciated!