I'm building an Angularjs application that needs to access a logged-in user's information within several different modules (user profile, liking posts, commenting). I'm aware that if certain data needs to be shared between disparate parts of an Angularjs app, it's desirable to use a service
or factory
. But what about the $rootScope, like so?
.run(function($rootScope, userService) {
userService.getLoggedInUser()
.then(function(user){
$rootScope.loggedInUser = user; // Contains username, joined date, photo URL, etc.
})
})
If I need the user's info within any part of the app, I can simply fetch it from the root
<div >Username: {{ $root.loggedInUser.username }} </div>
...
alert($rootScope.username());
Does this expose the application in any negative way? I'm not introducing any sensitive information like passwords or email, but maybe I'm overlooking something obvious.
Any input is appreciated!