Value from $rootScope is not loaded properly

41 views Asked by At

So I'm basically trying to get a property from my $rootScope when the page loads. 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. Why is this?

See console screen below.

enter image description here

Here is where I fill the localClient object

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

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

There are 2 answers

5
Sajeetharan On

Try accessing it like this,

console.log("DEBUG $rootScope.localClient", $rootScope['localClient']);
2
Nick Wang On

You must make sure the attribute loaded before use it, because JavaScripte always pass a reference to an object. Or you can try console.log(JSON.parse(JSON.stringify($rootScope)) get the real value.

One example: var a = {}; console.log(a);a.test = '1'; enter image description here