简体   繁体   English

$ rootScope中的值未正确加载

[英]Value from $rootScope is not loaded properly

So I'm basically trying to get a property from my $rootScope when the page loads. 因此,我基本上是在页面加载时尝试从$rootScope获取属性。 I need this property so I can display the value in my form. 我需要此属性,以便可以在表单中显示值。

After testing this: 经过测试之后:

console.log("DEBUG $rootScope", $rootScope);
console.log("DEBUG $rootScope.localClient", $rootScope.localClient);

I've noticed that $rootScope contains a localClient property, but $rootScope.localClient is undefined. 我注意到$rootScope包含一个localClient属性,但是$rootScope.localClient是未定义的。 Why is this? 为什么是这样?

See console screen below. 请参阅下面的控制台屏幕。

在此处输入图片说明

Here is where I fill the localClient object 这是我填充localClient对象的地方

function setClient(client, tvaNumber) {
    if (tvaNumber) {
        if (angular.isUndefined($rootScope.localClient))
            $rootScope.localClient = {};

        $rootScope.localClient[tvaNumber] = client; 
    }
}

尝试像这样访问它,

console.log("DEBUG $rootScope.localClient", $rootScope['localClient']);

You must make sure the attribute loaded before use it, because JavaScripte always pass a reference to an object. 在使用属性之前,必须确保已加载该属性,因为JavaScripte始终将引用传递给对象。 Or you can try console.log(JSON.parse(JSON.stringify($rootScope)) get the real value. 或者,您可以尝试console.log(JSON.parse(JSON.stringify($ rootScope))获得​​真正的价值。

One example: var a = {}; console.log(a);a.test = '1'; 一个例子: var a = {}; console.log(a);a.test = '1'; var a = {}; console.log(a);a.test = '1'; 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM