Reputation: 545
i have a service written to get data from a server. the server is running locally for testing purpose. the problem that i ma facing is that the data is returned inside a $$state if $http is written in a Service. if same $http is written in a Controller i get the data.
$$state is not returing any data if i type res.$$state.value.dat
aggregate.service('someService',function ($http)
{
var yearlyAPI = "http://localhost:3000/api";
var yearly =[];
return {
getYearlyAgg : function()
{
if(yearly.length!=0)
{
log("Returning Stored Data For yearly Agg");
return yearly;
}
else
{
var startDate = "2015-08-01";
var endDate = "2015-08-04";
var params = {};
params.startDate = startDate;
params.endDate = endDate;
var yearData = $http.get(yearlyAPI,{
params :params
});
yearly=yearData;
log("Returning New Data For yearly Agg");
log(yearData);
return yearData;
}
}
}; /*Return Ends*/
});
Upvotes: 0
Views: 2033
Reputation: 3211
Change you code and try this:
aggregate.service('someService',function ($http,$q)
{
var yearlyAPI = "http://localhost:3000/api";
var yearly =[];
return {
getYearlyAgg : function()
{
if(yearly.length!=0)
{
console.log("Returning Stored Data For yearly Agg");
return $q.when(yearly); //with this you will return a promise if yearly variable contain some object
}
else
{
var startDate = "2015-08-01";
var endDate = "2015-08-04";
var params = {};
params.startDate = startDate;
params.endDate = endDate;
return $http.get(yearlyAPI,{
params :params
}).then(function(result){
yearly.push(result.data); //here you will add your new value to yearly array
return result.data; //you are returning new data after you stored it in yearly variable
});
}
}
}; /*Return Ends*/
});
Please share your feedback with us.
Upvotes: 2