0
.controller('FrameCtrl', ['$scope','$state','$rootScope', '$stateParams',
    function($scope, $state, $rootScope, $stateParams) {

        console.log($rootScope); // all the data is there.
        console.log($rootScope.activeAddress); // undefined
        console.log($rootScope.activeRestaurant); // undefined   

}])

I am trying to understand why, even though the $rootScope object holds both the activeAddress and activeRestaurant object, they are empty when I console.log() them specifically.

In the controller of the previous state, I set these two objects using these two lines of code, if that makes a difference:

// Parse.com promise returns array of addresses after login.
$rootScope.activeAddress = data.addresses[0];
// After getting restaurant ID from activeAddress, copy object to activeRestaurant
$rootScope.activeRestaurant = $rootScope.aRestaurant;

In short, I want to know why $rootScope.activeAddress and $rootScope.activeRestaurant return undefined, even though they appear in the $rootScope parent object.

EDIT: Here is the markup from the HTML page of the previous state:

<button class="button button-block button-positive" ng-click="login()" 
    ui-sref="frame.menu({menu: 'appetizers'})">
    LOG IN
</button>
Caleb Faruki
  • 2,577
  • 3
  • 30
  • 54
  • I noticed that when you output something using `console.log`, the objects appear to have properties that have been set after `console.log` was called. Try cloning `@rootScope` and output the clone and you will see that it does not have these properties. for example: http://stackoverflow.com/questions/22146684/console-log-a-javascript-object-class-same-result-before-and-after-the-change – akonsu Jan 22 '15 at 18:01
  • How you change the state ..?? – squiroid Jan 22 '15 at 18:01
  • @squiroid I added the HTML – Caleb Faruki Jan 22 '15 at 18:14
  • 1
    try to print them using `$timeout(function(){console.log($rootScope.activeAddress); console.log($rootScope.activeRestaurant);})` – Pankaj Parkar Jan 22 '15 at 19:45

1 Answers1

1

This is because Angular loads A-sync and the controller loads before the rootScope. Try looking at defer() and promises with $q

Mike T
  • 11
  • 1