Angularjs 应该从 firebase 返回 $rootScope 数据吗? [英] Angularjs should I $rootScope data returned from firebase?

查看:21
本文介绍了Angularjs 应该从 firebase 返回 $rootScope 数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想从 firebase 检索用户信息,并且此用户信息将显示在多个路由/控制器中

Let say I want to retrieve user info from firebase, and this user info will be displayed in several routes/controllers

我应该$rootScope返回的用户信息吗?

Should I $rootScope the returned user info?

在每个控制器中调用下面的代码?

Call below code in each controller?

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

更新

我有一个带有 getUserInfo() 函数的以下服务,那么最好的方法是什么在几个控制器中使用它?在每个控制器中调用 firebaseAuth.getUserInfo().then() ?如果用户数据我必须在几个控制器中使用.为什么我不设置它$rootScope?所以我不需要在不同的控制器中一遍又一遍地调用它.

I have a following service with a getUserInfo() function then what is the best way to use it in several controllers? calling firebaseAuth.getUserInfo().then() in each controller? If the user data I have to use in several controller. Why don't I set it $rootScope? So I don't 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;
  }
});

推荐答案

AngularFire 的重点是让您的 javascript 数据模型始终与 Firebase 保持同步.您不希望每次需要获取数据时都创建一个新的 AngularFire 承诺.您只需初始化 AngularFire 一次,您的本地数据将始终是最新的.

The point of AngularFire is to keep your javascript data model in sync with Firebase at all times. You don't want to create a new AngularFire promise every time you need to fetch data. You just 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
        }
    }
}]);

请记住,您的服务的所有属性(即您使用 this 关键字分配的所有内容)都可以从注入此服务的控制器访问.因此,您可以执行诸如 console.log(firebaseAuth.userInfo)firebaseAuth.userRef.on('value', function(snap) { ... }); 之类的操作

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

此外,您可能最终希望使用 FirebaseAuthClient用于您的用户身份验证.

Also, you may eventually want to use the FirebaseAuthClient for your user authentication.

这篇关于Angularjs 应该从 firebase 返回 $rootScope 数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆