3

I'm creating a web app with angular. Users can log in and send messages to other users.

What's the best way of globally storing information about the logged in user - e.g. their username, ID, picture URL, etc? Or do I need to send it as part of the JSON data used to render each page?

2
  • Hi, good question, even though it is not a good practice to store anything in the $rootScope, this (sort of) global stuff can reside in there. So, inject $rootScope to any of your controllers, like: YourModule.controllers('myCtrl, function ($scope, $rootScope)') and it will be accessible to any controller. Or you can use a separate service for that purpose as well... Commented Mar 9, 2013 at 13:20
  • Dont litter the $rootScope. He might as well just store the info on window... Commented Mar 9, 2013 at 13:22

1 Answer 1

4

The "Angular" way to do this, is to use something called a Service

See this video by the amazing John Linguist, for more information on how to achieve this

http://egghead.io/video/angularjs-sharing-data-between-controllers/

Sign up to request clarification or add additional context in comments.

1 Comment

Link is not working anymore, here's the new one egghead.io/lessons/angularjs-sharing-data-between-controllers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.