I\'m sure this will be an easy one for any Angular/Javascript experts. I have a service that makes a call to an API to get some data:
app.service(\"GetDivisi
You need to understand that you can't return from asynchronous operations. Response is simply not yet available when you try to return data. Typically in Javascript you would use callback or promise pattern. In Angular promise is a very natural choice.
app.service("GetDivision", ["$http", function($http) {
this.division = function(divisionNumber){
return $http.post("/api/division", {division:divisionNumber}).success(function(data){
return data;
});
}
}]);
and in controller:
GetDivision.division(1).then(function(data) {
$scope.division = data;
});
Make sure you read this very popular thread on the subject: How do I return the response from an asynchronous call?