如何 return 转换来自 angular 中 $http.json 请求的数据?

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

如何使用 $http.jsonp return APIData.map 而不是默认成功 APIdata

LangDataService 构造函数:

 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.
         });
      });
};

使用 LandDataService 的 Ctrl:

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

getApi 函数中使用 then 而不是 success

尝试以下版本: https://jsfiddle.net/L2ndft4w/

// define the module for our AngularJS application
var app = angular.module('App', []);

app.factory('LangDataService', function($q,$http) {

  return {
    getApi: function() {
      var defer= $q.defer();
      $http({
          url: 'https://mysafeinfo.com/api/data?list=zodiac&format=json&alias=nm=name',
          dataType: 'json',
          method: 'GET',
          data: '',
          headers: {
              "Content-Type": "application/json"
          }
      }).
      success(function (data) {

          defer.resolve(data.map(function(item){
            return item.name;
          }));
      })    
      return defer.promise;
    }  
  };
});

// invoke controller and retrieve data from $http service
app.controller('DataController', function ($scope, LangDataService) {
  LangDataService.getApi().then(function(data){
    $scope.data = JSON.stringify(data, null, 2);
  });
});

Returns:

[
  "Aquarius",
  "Aries",
  "Cancer",
  "Capricorn",
  "Gemini",
  "Leo",
  "Libra",
  "Ophiuchus",
  "Pisces",
  "Sagittarius",
  "Scorpio",
  "Taurus",
  "Virgo"
]

不过,因为 $http 已经是一个承诺,所以可能有更短的方法来做到这一点...($q.when?)