Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

General Discussion

medev21
medev21
18,291 Points

Unable to get $rootscope object on pageload in angularjs

I am using ng-token-auth and devise token-auth in a rails app. Trying to the user object from $rootscope so I can get the user data such as username, first name, last name, etc.

I'm a total noob to AngularJS, so I'm not sure how to solve this

I noticed when i do => console.log($rootScope); I see the user object output => Object {$id: 1, $$childTail: Object, $$childHead: Object, $$prevSibling: null, $$nextSibling: null…}

but when i do console.log($rootScope.user), i get undefined in the output

I did $rootScope.watch and I was able to get the username and set it as local storage, but i believe this is bad practice

Is there a better way to get the user object so i can use it across the platform? Please help!