使用jQuery函数返回值

时间:2015-05-19 05:09:15

标签: jquery angularjs return http-get

如何从以下函数中获取返回值,以便我可以在角度控制器中使用返回值:

var demoService = angular.module('demoService', [])
    .service('myService', function($http, $q) {
        var deferred = $q.defer();
        $http.get('http://enlytica.com/RSLivee/rest/census').then(function(data) {
            deferred.resolve(data);
        });


        this.getdata = function() {
            var dataarr = [];
            var v = 0;

            var datarec = function datafetch() {
                $http.get('http://enlytica.com/RSLivee/rest/census').then(function(data) {
                    var v = data.data.Tweets[0].FAVOURITE_COUNT;
                    console.log(data.data.Tweets[0]);
                    return (({
                        maxValue: 4000,
                        animationSpeed: 100,
                        val: v
                    }));
                });
            }
            var datarec = datafetch();
            console.log(datarec);
            return datarec;
        }

    })

1 个答案:

答案 0 :(得分:0)

我认为这会按预期运作:

 this.getdata = function() {
   return $http.get('http://enlytica.com/RSLivee/rest/census').then(function(data) {
     var v = data.data.Tweets[0].FAVOURITE_COUNT;
     return {
       maxValue: 4000,
       animationSpeed: 100,
       val: v
     };
   });
 }

从控制器使用服务:

//The result of service.getdata() is just a promise, you can chain it howerver you like
service.getdata().then(function(data){
//The data here should be: {  maxValue: 4000, animationSpeed: 100, val: v}
 console.log(data); 
});