1

I have a $scope.currentUser object and a nested OwnedCompany object like so:

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

{{currentUser}} returns:

user: { 
  "name": "Ryan",
  "owned_company":{"name":"XYZ"}
}

{{currentUser.owned_company}} returns:

{"name":"XYZ"}

but {{ownedCompany}} returns nil.


How do I get $scope.ownedCompany = $scope.currentUser.owned_company to work?


Solution

As per @mohamedrias' answer,

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

{{ownedCompany}} returns:

{name:"XYZ"}
5

1 Answer 1

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

It's an async call from where you're getting the current user details. So it will resole later.

Either you must use promise ad put

  $scope.ownedCompany = $scope.currentUser.owned_company

Inside the then block.

Else register a watch on that object and perform the assignment.

Sign up to request clarification or add additional context in comments.

1 Comment

This works great. I'll add the solution to my question. Thanks Mohamed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.