我有一个$ scope.currentUser对象和一个嵌套的OwnedCompany对象,如下所示:
app.controller 'dashboardsCtrl', ($scope, Api, Auth) ->
$scope.currentUser = Api.User.get(id: Auth.user())
$scope.ownedCompany = $scope.currentUser.owned_company
{{currentUser}}
返回:user: {
"name": "Ryan",
"owned_company":{"name":"XYZ"}
}
{{currentUser.owned_company}}
返回:{"name":"XYZ"}
但是
{{ownedCompany}}
返回nil。如何使
$scope.ownedCompany = $scope.currentUser.owned_company
工作?解
按照@mohamedrias的回答,
app.controller 'dashboardsCtrl', ($scope, Api, Auth) ->
Api.User.get(id: Auth.user()).$promise.then (currentUser) ->
$scope.currentUser = currentUser
$scope.ownedCompany = currentUser.owned_company
{{ownedCompany}}
返回:{name:"XYZ"}
最佳答案
$scope.currentUser = Api.User.get(id: Auth.user())
这是一个异步调用,您可以从中获取当前的用户详细信息。因此,它将在以后重播。
您必须使用Promise广告投放
$scope.ownedCompany = $scope.currentUser.owned_company
在
then
块中。否则在该对象上注册
watch
并执行分配。