I want to know if it is possible to make a service call that uses $http
so it returns data directly without returning a promise? I have tried to use the $q
and defer without any luck.
Here is what I mean:
I have a service:
angular.module('myModule').factor('myService', ['$http', '$q',
function($http, $q) {
// Public API
return {
myServiceCall: function() {
return $http.get('/server/call');
}
};
}
]);
And this is how I would call it:
// My controller:
myService.myServiceCall().then(function(data) {
$scope.data = data;
});
I want to avoid that and instead wish to have:
$scope.data = myService.myServiceCall();
I want it to be fully resolved at that line, is it possible?
I have tried some combinations of $q, defer and 'then' methods but can't seem to get it right since the method returns immediately.
Edit:
If you are wondering why, the main reason is that I wanted to simplify the controller code, this is easily done with ngResource as those calls are automatically resolved in templates, so I wanted to avoid the need to do the the whole '.then' everytime.
It's not that I don't like the Async nature, most of our code leverages it, it's just in some cases, having a synchronous way is helpful.
I think for now as some of you have pointed out, I will use the $resource for a near enough solution.
$resource
fromngResource
which does some funky internal promise resolution though, eg$scope.data = $resource('/server/call').get()
– Gentes