0

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; 
    }
}
Jordec
  • 1,516
  • 2
  • 22
  • 43

2 Answers2

0

Try accessing it like this,

console.log("DEBUG $rootScope.localClient", $rootScope['localClient']);
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

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

Nick Wang
  • 624
  • 3
  • 6
  • [JavaScript console prints assigned value of variable before it has been assigned?](https://stackoverflow.com/q/8747421/2435473) – Pankaj Parkar Jan 18 '19 at 09:04
  • yes, but you still can see it in debug information. So, it is why you can see localClient in debug panel, but it is undefined if you try use it. – Nick Wang Jan 18 '19 at 09:08