无法访问Angular $ rootScope对象属性

时间:2015-11-07 07:49:51

标签: javascript angularjs 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)console.log($rootScope.globals.currentUser)一切都很好。

但是当我尝试打印console.log($rootScope.globals.unread)时,输出为undefined

为什么呢?我认为如果属性值等于undefined,则在其他输出中不应该看作0。我错了吗?

这里有什么问题?

1 个答案:

答案 0 :(得分:0)

我发现你的代码没有任何问题,可能你没有注入$ rootScope,但这里有工作Plunker

控制器代码:

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) 
}]);
相关问题