Angularjs should I get the root root data received from firebase?

Let's say I want to get user information from firebase, and this user information will be displayed in several routes / controllers

Should I $rootScope return user information?

or

Call under code in each controller?

 firebaseAuth.firebaseRef.child('/people/' + user.id).on('value', function(snapshot) { $scope.user = snapshot.val(); }) 

UPDATE

I have the following service with getUserInfo() function, then what is the best way to use it in multiple controllers? call firebaseAuth.getUserInfo().then() in each controller? If user data I have to use in several controllers. Why don't I install it with $rootScope ? Therefore, I do not need to call it again and again in different controllers.

 myapp.service('firebaseAuth', ['$rootScope', 'angularFire', function($rootScope, angularFire) { this.firebaseRef = new Firebase("https://test.firebaseio.com"); this.getUserInfo = function(id) { var userRef = this.firebaseRef.child('/human/' + id); var promise = angularFire(userRef, $rootScope, 'user', {}); return promise; } }); 
+1
source share
2 answers

The AngularFire point should constantly support your javascript data model in sync with Firebase. You do not want to create a new AngularFire promise every time you need to get data. You simply initialize AngularFire once, and your local data will always be up to date.

 myapp.service('firebaseAuth', ['angularFireCollection', function(angularFireCollection) { this.firebaseRef = new Firebase("https://test.firebaseio.com"); this.initUserInfo = function(id) { if (!this.userRef) { this.userRef = this.firebaseRef.child('/human/' + id); this.userInfo = angularFireCollection(this.userRef); } else { // already initialized } } }]); 

Remember that all the properties of your service (i.e. everything that you assign using the this ) are accessible from the controllers injected by this service. This way you can do things like console.log(firebaseAuth.userInfo) or firebaseAuth.userRef.on('value', function(snap) { ... });

Alternatively, you can end up using FirebaseAuthClient to authenticate the user.

+1
source

I would recommend creating a service for authenticating and storing user data. Then you can enter the service into any controller that needs access to the user.

+1
source

All Articles