Thomas
Thomas

Reputation: 5099

AngularJS : Service not Returning Variables to Controllers

My app has a service that is supposed to return an array of objects (tweets from the twitter api) that looks like:

.service('twitterService', function($http) {
    this.getTweets = function(){
        $http.get('http://example.com/?json=get_twitter_sidebar').success(
            function(data, status, headers, config) {
                if (data.status == 'ok') {
                    return data.feed;
                } else {
                    return 'Error Retrieving Feed'
                }
            });
    }

})

This service seems to work; it calls the api and returns the desired object array (I used console.log to verify). However, I can't seem to pass the returned array to my controller:

var TopPageCtrl = function($scope, $window, $http, twitterService) {
    $scope.feed = twitterService.getTweets();
};

When I use console.log($scope.feed), it returns undefined. I've tried a million approaches but nothing seems to work. What am I doing wrong?

Upvotes: 0

Views: 605

Answers (3)

Rodrigo Fonseca
Rodrigo Fonseca

Reputation: 944

$scope.feed = twitterService.getTweets();

this.getTweets = function(){
            $http.get('http://example.com/?json=get_twitter_sidebar').success(
                function(data, status, headers, config) {
                    if (data.status == 'ok') {
                        return data.feed;
                    } else {
                        return 'Error Retrieving Feed'
                    }
                });
        }

Your function isn´t returning anything, that´s why you are getting "undefined".. Try this:

this.getTweets = function(){
                return $http.get('http://example.com/?json=get_twitter_sidebar').success(
                    function(data, status, headers, config) {
                        if (data.status == 'ok') {
                            return data.feed;
                        } else {
                            return 'Error Retrieving Feed'
                        }
                    });
            }

twitterService.getTweets().then(function (response){...});

Upvotes: 1

James
James

Reputation: 386

return the promise

.service('twitterService', function($http) {
this.getTweets = function(){
    var p = $http.get('http://example.com/?json=get_twitter_sidebar').success(
        function(data, status, headers, config) {
            if (data.status == 'ok') {
                return data.feed;
            } else {
                return 'Error Retrieving Feed';
            }
        });
     return p;
}});


var TopPageCtrl = function($scope, $window, $http, twitterService) {
twitterService.getTweets().then(function (response) {
  $scope.feed = response;
};};

Upvotes: 2

tarrball
tarrball

Reputation: 2298

Maybe it's returning a promise that needs to be resolved? Try something like...

var getFeed = twitterService.getTweets();
getFeed.then(function (feed) {
    $scope.feed = feed;
});

Upvotes: 1

Related Questions