如何遍历AngularJS范围元素的子元素

时间:2015-11-05 06:38:01

标签: angularjs angularjs-scope

我正在尝试一些Angular,我似乎无法理解$scope个对象。

我似乎无法轻松遍历范围对象子。我有一个match对象,其中players我想循环以获取他们的分数。

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");
  });
});

这里没有返回任何内容,没有任何价值或测试,我确信$ scope.match有3名玩家。

任何人都知道我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

好像你正在Matches.get()进行服务器端呼叫。 A JAX请求是异步的,因此您不会在代码执行后立即获得结果。所以改变你的代码:

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");
    });
});