0

我正在尝试从这个 url http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo获取数据

通过下面的 $resource 是我的资源代码

angular.module('myApp.services', ['ngResource']).
 value('version', '0.1').factory('recipes1',['$resource', '$http', '$log', function  ($resource, $http, $log) {
return $resource('http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo',{ },{   locate: {method: 'GET', isArray: true, transformResponse: $http.defaults.transformResponse.concat(function(data, headersGetter) {
  // probably be better if you examined the results in here
alert(data);

 })}
});
}]);

但我没有得到回应。我从我的控制器中出来

function Resource(value){
"use strict";

    copy(value || {}, this);
  } 
4

1 回答 1

0

$http与承诺工厂一起使用:

见工作演示fiddle

JS

var fessmodule = angular.module('myModule', ['ngResource']);

fessmodule.controller('fessCntrl', function ($scope, Data) {

    $scope.runMe = function () {

        Data.query($scope.url)
                        .then(function (result) {
                           $scope.data = result;

                        }, function (result) {
                            alert("Error: No data returned");
                        });    
    }

});

fessmodule.$inject = ['$scope', 'Data'];

fessmodule.factory('Data', ['$http','$q',  function($http, $q) {

     var data = $http({method: 'GET', url: 'http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo'});

       var factory = {
            query: function (address) { 
                var deferred = $q.defer();              
                deferred.resolve(data);               
                return deferred.promise;
            }
        }
        return factory;
}]);
于 2013-10-31T09:46:34.247 回答