访问数组中的元素

时间:2013-02-18 08:33:39

标签: arrays angularjs

我有一个控制器功能如下

function UserDetailsControl($scope, $routeParams, UserService) {
    $scope.user = UserService.get({id: $routeParams.id});
    alert($scope.user.phone);
}

当我访问模板中的用户变量时,它看起来如下所示:

{
  "website_name": null,
  "email": "Monroe@Chamorro.com",
  "first_name": "Monroe",
  "id": "31",
  "last_name": "Chamorro",
  "phone": 2147483647,
}

alert($scope.user.phone);这总是返回undefined。为什么?无论我尝试访问哪个元素,它总是返回undefined

1 个答案:

答案 0 :(得分:1)

因为$ resource是异步的,所以会发生这种情况。请查看此主题以获取详细的解释:How does the $resource `get` function work synchronously in AngularJS?