What is the best way to store a "user" object that is accessible across all controllers in an Angular application?
I also have a function that fetches & sets the user.
Option 1: $rootScope
At first I was just setting $rootScope.user = myUser
. Then in every control I could use $rootScope. This was fine, until I ended up with a lot of duplicate code since I had to check if it existed at the start of each controller.
// At the start of each controller
if (!$rootScope.user) {
// fetch and set user
}
Option 2: Angular service?
Option 3: Angular factory?
References: angular.service vs angular.factory