0

我有一个控制器功能如下

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

4

1 回答 1

1

这是因为 $resource 是异步的。检查此线程以获取详细说明:$resource `get` 函数如何在 AngularJS 中同步工作?

于 2013-02-18T08:43:09.367 回答