-1

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?

Furkan Başaran
  • 1,927
  • 2
  • 19
  • 28
  • I dnt find any problem. it should work – Sajeetharan Nov 07 '15 at 08:18
  • @Sajeetharan I think so but not working – Furkan Başaran Nov 07 '15 at 08:20
  • @Furkan, do you see the value as o for `unread` when printing `console.log($rootScope.globals)` – M22an Nov 07 '15 at 08:21
  • Try using Batarang, that would be really helpful in these cases. https://chrome.google.com/webstore/detail/angularjs-batarang/ighdmehidhipcmcojjgiloacoafjmpfk?hl=en – M22an Nov 07 '15 at 08:25
  • @M22an yes when i printing `console.log($rootScope.globals)` I can see unread's value but when i try `console.log($rootScope.globals.unread)` I got `undefined`. – Furkan Başaran Nov 07 '15 at 08:33
  • Are you trying both console.log in the same controller ? – M22an Nov 07 '15 at 08:37
  • Yes I try this in the same controller – Furkan Başaran Nov 07 '15 at 08:38
  • Then unless the `$rootScope.globals` was modified in between these two `console.log()` requests, there is no reason for this error to happen. – M22an Nov 07 '15 at 08:52
  • Where's the code that generates this issue? A demo in [plunker](http://plnkr.co/edit/?p=catalogue) that replicates the problem would also help. Nobody can do much to help you with what little is provided in your question – charlietfl Nov 07 '15 at 09:29
  • Obviously the reason is to be found in the information you don't gave us. A wild guess: `undefined` is a strong indication that the property doesn't exist. Maybe there's a typo and `globals` actually has a property named `unred`, for instance, or the other way round. – a better oliver Nov 07 '15 at 11:48

1 Answers1

0

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) 
}]);
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396