I'm currently facing an issue where I am trying to assign a variable to one retrieved from a controller through a promise obtained from a factory. This factory fetches 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);
});
Even though within the .then
function true is returned, the value of $scope.m
remains undefined.
If anyone has any insights or suggestions on how to resolve this, your input would be highly appreciated!