Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return Values from AngularJS services

I am trying to return a value from my AngularJs service to my controller where ever I pass it inside the controller.It does pass the object and also log the data in the console. Here is my code:

angular.module('demoService',[])
.factory('myService',
function($http) {
return {
    getAll: function (items) {
        var path = "http://enlytica.com/RSLivee/rest/census"
        $http.get(path).success(function (items) {

        console.log(items.Tweets[1].FAVOURITE_COUNT);

       });
    }
}
});

How do i return the "items.Tweets[1].FAVOURITE_COUNT" on function call.

Thanks in advance

like image 926
rawatdeepesh Avatar asked Jul 02 '26 08:07

rawatdeepesh


1 Answers

You can create a promise of the data queried, by using the method then , getAll will know what to do before the response of the API.

angular.module('demoService')
.factory('myService', ['$http', 
    function ($http) {

        function getAll (items) {
            var path = "http://enlytica.com/RSLivee/rest/census"
            return $http.get(path).then(
                function (res) {
                    return res;
                 });
        }

        return {
            getAll: getAll;
        }

    }

}];
like image 153
Vincent Taing Avatar answered Jul 04 '26 02:07

Vincent Taing