来自嵌套对象的Angularjs $ scope.object

时间:2015-05-04 05:13:31

标签: javascript angularjs

我有一个$ scope.currentUser对象和一个嵌套的OwnedCompany对象,如下所示:

one.Zip(two,(o1, o2) => new One() 
{ 
   ID = o1.ID, 
   Name = o1.PCName, 
   LastName = o1.LName, 
   MainName = o2.mName 
});

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"} 返回nil。

如何让{{ownedCompany}}工作?

解决方案

根据@mohamedrias的回答,

$scope.ownedCompany = $scope.currentUser.owned_company

app.controller 'dashboardsCtrl', ($scope, Api, Auth) -> Api.User.get(id: Auth.user()).$promise.then (currentUser) -> $scope.currentUser = currentUser $scope.ownedCompany = currentUser.owned_company 返回:

{{ownedCompany}}

1 个答案:

答案 0 :(得分:2)

  $scope.currentUser = Api.User.get(id: Auth.user())

从您获取当前用户详细信息的位置进行异步调用。所以它稍后会解决。

您必须使用promise ad put

  $scope.ownedCompany = $scope.currentUser.owned_company

then区块内。

否则在该对象上注册watch并执行分配。