I am in the process of retrieving data from my database using AngularJS. I have created a service to fetch the data and a controller to display it.
Check out my code snippet:
angular.module('myApp')
.factory('panelService', ['$http', function ($http) {
return {
async: function () {
return $http.get('/test'); // this returns promise
}
};
}]);
angular.module('myApp')
.controller('panelCtrl', ['$scope', '$http', function ($scope, $http) {
var promise = panelService.async()
promise.then(
function(payload){
$scope.user = payload.data;
}
)
}]);
Regrettably, this code is not functioning as expected. The data is not being loaded and there is no sign of any JSON object reaching the browser. Interestingly, when I replace panelService.async()
with $http.get('/test')
in my controller, everything works perfectly. This leads me to believe that there may be an error in my service implementation or it is not being called correctly.