Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a big problem in my angular js application. I have a factory module which has a getAll() function, this get the json data from the server. And I have the controller module where I try to assign the factory's getAll() function's value to the scope.eventSports.

My problem is that, the variable assign is run first, and it does not wait that the getAll() function return back with the data. The variable assign run at first with the undefinied... and then I get the result of getAll() function.

How can I achieve the variable assign wait the getAll() function?

The factory:

var gameDataFactory = angular.module('gameDataFactory', []);

gameDataFactory.factory('gameDataFactory', ['gameService', function(gameService) {

    var sportEvents = {

        getAll : function(){
            gameService.getGroupedEvents()
            .success(function(data) {
                // Get the json data from the server.
                // This gives back the data, but later, this run at second time...
                console.log(data.sportEvents);
                return data.sportEvents;
            })
            .error(function(error) {
                return null;
            });
        }
    };

    return {
        sportEvents: sportEvents
    };
}]);

The controller:

gameControllers.controller('SportEventListCtrl', ['$scope', 'gameService', 'gameDataFactory', '$sce',
    function($scope, gameService, gameDataFactory, $sce) {
    $scope.sportEvents = {};

    $scope.status = true;

    // I should somehow assign this scope variable with callback
    $scope.sportEvents = gameDataFactory.sportEvents.getAll();
    // This run first and this is undefinied.
    console.log($scope.sportEvents); 
share|improve this question
    
getGroupedEvents() has an async call? I think that the best way to handle this, is using promises. Take a look here markdalgleish.com/2013/06/using-promises-in-angularjs-views and here andyshora.com/promises-angularjs-explained-as-cartoon.html –  Chris Benseler 20 hours ago
    
    
betDataFactory.sportEvents.getAll().then(function(data){ $scope.sportEvents = data; console.log("in " + $scope.sportEvents); }); I tried this. But I got an error: TypeError: Cannot read property 'then' of undefined –  Lacces 20 hours ago
    
You should probably have a look at How to return the response from an Ajax call?. –  Felix Kling 20 hours ago
    
@Lacces: You are not returning anything from getAll, so of course that doesn't work. –  Felix Kling 20 hours ago

2 Answers 2

up vote 1 down vote accepted

Change this line

gameService.getGroupedEvents()

to

return gameService.getGroupedEvents()
share|improve this answer

There are several ways of implementing this functionality, such as returning the deferred object. Another simpler approach is to pass $scope to getAll function, and bind whatever data to the passed $scope.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.