我有一个$ 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}}
返回零。
我该如何获得$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"}
http://jsfiddle.net/arunpjohny/jpno6ft2/1/ –
你确定写变量名没有错?或者在字符外壳? –
如果'currentUser.owned_company'在运行时更改,则使用watch或在更改发生后设置值 - http://jsfiddle.net/arunpjohny/jpno6ft2/2/ –