I have a factory where I am pulling data from an API and trying to assign the results to a variable. Everytime I run, I get my variable is undefined. Any way in which I can pass the results to my variable from an async call? For instance in my case. My factory looks like this.
angular.module('MyApp.services', [])
.factory('ReportService', ['$http', '$window', '$upload', 'AuthService', function ($http, $window, $upload, AuthService) {
return {
findAll: function (criteria) {
criteria = criteria || [];
return $http.get(BASE_URL + '/ajax.php?action=reports.all&' + criteria.join('&'));
}
}
}])
then in my controller
.controller('MyViewController', [
'$scope', 'ReportService', 'toaster', '$modal', '$rootScope',
function ($scope, ReportService, toaster, $modal, $rootScope) {
ReportService
.findAll()
.then(
function success(response, status, headers, config) {
$scope.reports = response.data.reports;
},
function error(response, status, headers, config) {
console.log('error');
});
//console.log($scope.reports) returns undefined here.
}
]);
How do I go about getting the variable populated at the global level for the controller?