Angularjs - Cannot Access Rootscope
Solution 1:
There are two key problems here: first, you are using $rootScope when you should be using a service, and second, you are assuming the order of execution of the controllers. If it was just a matter of the second, you could set up $watch commands to monitor changes to ensure your second controller had an updated value, whenever it was set.
But you shouldn't pollute the global scope. Wrap your token into a service that can be injected and on which you can $watch for changes. That said, I can't post anything more specific (i.e. code) without knowing in what context your controllers run.
But I hope this helps! Update your post with more info if you want and I'll dive a little deeper. Actually, it'd be great if you could create a Plunker or jsFiddle that I can directly modify.
Solution 2:
It's accessing the $rootScope
just fine. If it wasn't you'd be getting a javascript error about $rootScope not being defined. Instead, it's tacking "undefined" onto the end of the request URL, meaning $rootScope.token
is undefined. Do whatever you need to to make sure $rootScope.token
is defined.
I suspect CtrlB is getting called before CtrlA, and so $rootScope.token
hasn't been set yet.
Post a Comment for "Angularjs - Cannot Access Rootscope"