Search code examples
javascriptangularjsajaxxmlhttprequestjsonp

How to return transformed data from a $http.json request in angular?


How can I return the APIData.map and not the default success APIdata using $http.jsonp?

LangDataService Constructor:

 languages.LangDataService = function($http, $q) {
   this.langDefer = $q.defer();
   this.isDataReady = this.langDefer.promise;
 };


languages.LangDataService.prototype.getApi = function() {
    return this.isDataReady = this.http_.jsonp(URL, {
        params: {}
      })
      .success(function(APIData) {
        return APIData.map(function(item){
              return item + 1; //just an example.
         });
      });
};

A Ctrl using LandDataService:

languages.LanguageCtrl = function(langDataService) {
   languages.langDataService.isDataReady.then(function(data){
       console.log('whooo im a transformed dataset', data);
   });
}

Solution

  • Use then instead of success in getApi function.