I have a couple of questions on asynchronous functions in angular. I want my service function to be able to return data I get by using $http and use that data in another function. Let me demonstrate with the current code:
dashboard.servicesModule.service('SubscriptionService', function ($http, $q) {
this.getImportantData= function (param1, param2) {
var url = "my/url/with/parameters?param1=param1¶m2=param2";
$http.get(url).then(function(response){
console.log("response");
console.log(response.data); // < --- this actually shows the data i need!!
return response.data.vsyData; <--- however, this returns undefined
}, function(error){
// some error thingy just in case
});
};
this.getSomeFunctionality = function(param1, param2, param3){
var importantdata= this.getImportantData(param1, param2);
// do some filtering on the data with the purpose of returning only what i need
console.log(importantdata); <--- undefined
};
this.getEvenMoreFunctionality = function(param1, param2, param3){
var importantdata= this.getImportantData(param1, param2);
// reusing getImportantData again!
console.log(importantdata); <--- undefined
};
});
So I have been trying all kinds of things ( like this and this ) and some inventions of my own. But it starts to seem like there is no way of using the data from the $http.get anywhere else then in its own callback. And since i need the endresult in my controller, it seems like there is no other way then perform the $http.get(url).success(...some logic here) in my controller and do the filtering and other manipulation there.
However, I read here and I quote:
Our controller should be almost completely agnostic as to how data is retrieved and sent, and should only be concerned with the general action of sending and retrieving data to and from the service.
I interpret this as following: my controller should ask the service for the data I need and the service should make sure that it gives the data in the correct form.
The most important question is: how can I make the above code do what it needs to do or would that be impossible?
And am I supposed to do my business logic in my controller? Because here, in the angular docs I basically read that you should do business logic in your controller, but not filtering...
Its probably obvious, but ... I am an angular newbie :-). Thanks!