Skip to content Skip to sidebar Skip to footer

Use $rootScope To Store A Reference To A Service

I'm working on a medium / big size application where we have the current user data and methods stored on a Service. Because we need the user info across the application, would it b

Solution 1:

If you store it on $rootScope, you will be able to read it in any controller without injecting $rootScope due to prototypical inheritance, but the minute someone decides to make a new $scope.user property in one of your controllers, you'll lose your reference to the user property on $rootScope, unless you have explicitly injected and reference $rootScope directly. That could waste someone's time trying to debug.

I believe convention is that it's acceptable to store data on $rootScope but don't store functions there. I wouldn't rely on the prototypical inheritance from child scopes unless, at a minimum, you make a model object on $rootScope with a minimal chance of being overridden by a controller, like $rootScope.globalData.user or something.


Post a Comment for "Use $rootScope To Store A Reference To A Service"