Search code examples
javascriptangularjsrootscope

Angular $rootScope object property not accessible


My $rootScope.globals object is below:

{
 currentUser: {
    id: "56309272279724c02b319392"
    name: "Özgür Adem"
    picture: "http://www.gravatar.com/avatar/c4ca4238a0b23452sdcc503a6f76829z?  d=retro"
    ptoken: "mFFa9l25HbB4fbh7"
    role: Object
 },
 unread: 0
}

When I print to console like this console.log($rootScope.globals) or console.log($rootScope.globals.currentUser) everything good.

But when I try print console.log($rootScope.globals.unread), output being undefined.

Why? I think if property value equal to undefined, it shouldn't seen as 0 in other outputs. Am I wrong?

What's wrong here?


Solution

  • I don't find any problem with your code, may be you have not injected $rootScope but here is the working Plunker

    Controller code:

    App.controller('TodoController',['$scope','$rootScope',function($scope,$rootScope){
    
       $rootScope.globals = {
     currentUser: {
        id: "56309272279724c02b319392",
        name: "Özgür Adem",
        picture: "http://www.gravatar.com/avatar/c4ca4238a0b23452sdcc503a6f76829z?  d=retro",
        ptoken: "mFFa9l25HbB4fbh7",
        role: Object
     },
     unread: 0
    }
       console.log($rootScope.globals.unread) 
    }]);