1

I'm trying out some Angular and I can't seem to get my head around $scope objects.

I don't seem to be able to easily loop through a scope objects child. I have a match object, which has players where I want to loop through to get their points.

app.controller('PlayController', function($scope, Points, Matches, $routeParams, $location){
  var id = $routeParams.id;
  $scope.match = Matches.get({id: id});

  angular.forEach($scope.match.players, function(value, key) {
      console.log(value);
      console.log("test");
  });
});

Nothing is returned here, no value or test and I'm sure $scope.match has 3 players in there.

Anyone any idea what I'm doing wrong here?

1 Answer 1

1

Seems like you are doing a server side call in Matches.get(). An AJAX request is asynchronous so you will never get the results immediately after the code execution. So change you code like this:

var id = $routeParams.id;
$scope.match = Matches.get({id: id}, function(data) {   // Callback when your server responded the data
    console.log(data);

    angular.forEach($scope.match.players, function(value, key) {
       console.log(value);
       console.log("test");
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Legend! It seems I got so stuck with working with angular that I forgot something as basic as an AJAX call...
Sure, it happens :-) We are here to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.