1

In my main layout I have a heading, like:

<h1>{{ heading }}</h1>

I can set that to various static values from my controllers using:

$rootScope.heading = 'My Heading';

But if I try to create a dynamic heading from a resource, its blank:

app.controller('ShowAccountController', function($scope, $rootScope, $state, $stateParams, Account) {
  $scope.account = Account.get({ id: $stateParams.id });
  $rootScope.heading = $scope.account.name;
});

The account variable is definitely set there (checked via console) so I'm a bit stumped as to why the heading doesn't have a value?

2
  • 1
    Chrome's console intelligently updates the logged display value after the async call completes (apparently it passes a reference to the console) even though at the time of the log it is actually undefined because the async call hasn't completed yet. Commented Jun 19, 2013 at 3:33
  • @sh0ber How confusing! I'm sure that'll save me (and anyone reading this) some headache in the future. Thanks for the tip. Commented Jun 19, 2013 at 8:54

1 Answer 1

1

As @sh0ber pointed out you are being confused be the way Chrome console works. Anyway, to set the heading variable you should use callback:

app.controller('ShowAccountController', function($scope, $rootScope, $state, $stateParams, Account) {
  $scope.account = Account.get({ id: $stateParams.id }, function(account){
    $rootScope.heading = account.name;
  });
});
Sign up to request clarification or add additional context in comments.

Comments

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.