虽然它的存在不能访问$ rootScope变 [英] Can't access $rootScope Variable although it's there
问题描述
当我这样做是在Chrome与AngularJS:
When I do this in Chrome with AngularJS:
$urlRouterProvider.otherwise(function($injector, $location){
var $state = $injector.get("$state");
var $rootScope = $injector.get("$rootScope");
console.log($rootScope);
console.log($rootScope.isAuthenticated);
}
然后在控制台中我得到
Then in the console I get
打开第一行,我看到isAuthenticated设置(无论是或真或假)。正如预期的那样。
Open the first line and I see the "isAuthenticated" is set (either false or true). As expected.
但是,第二行给了我一个未定义
。 WTF?
But the second line gives me an undefined
. WTF?
推荐答案
所以,感谢@Macha我得到了答案。
So thanks to @Macha I got the answer.
到时候我扩大在Chrome对象我的code填充的对象。在code以上的app.config
运行。后来isAuthenticated获取 app.run
设置。
By the time I expand the object in Chrome my code populates the object. The code above runs in app.config
. Later the isAuthenticated gets set in app.run
.
这意味着这两个行是快照,但是当我展开对象,让Chrome当前内容 - 这也解释了两行之间的差异
This means that both lines are snapshots but when I expand the object, Chrome gets the current content - which explains the difference between the two lines.
您可以通过添加调试器看到这种情况;
是这样的:
You can see this by adding debugger;
like this:
console.log($rootScope); debugger;
然后寻找到的对象,而code是暂停。这几乎使我的一些毛发。
And then looking into the object while the code is on pause. This almost cost some of my hairs.
这篇关于虽然它的存在不能访问$ rootScope变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!