我有一个控制器功能如下
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
。
答案 0 :(得分:1)
因为$ resource是异步的,所以会发生这种情况。请查看此主题以获取详细的解释:How does the $resource `get` function work synchronously in AngularJS?