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

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

问题描述

让说我想要从火力用户信息,
而这个用户信息将显示在几条航线/控制器

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

呼叫低于code每个控制器?

  firebaseAuth.firebaseRef.child('/人/+ user.id)。在(价值,功能(快照){
  $ scope.user = snapshot.val();
})

更新

我有一个 getUserInfo()功能的以下服务那么什么是最好的方法
使用它在几个控制器?
调用 firebaseAuth.getUserInfo()。那么()每个控制器?
如果用户数据我有几个控制器使用。我为什么不把它 $ rootScope
所以,我并不需要在不同的控制器一次又一次地调用它。

  myapp.service('firebaseAuth',['$ rootScope','angularFire',函数($ rootScope,angularFire){
  this.firebaseRef =新的火力地堡(https://test.firebaseio.com);
  this.getUserInfo =功能(ID){
    VAR userRef = this.firebaseRef.child('/人/'+ id)的;
    VAR承诺= angularFire(userRef,$ rootScope,用户,{});
    返回的承诺;
  }
});


解决方案

AngularFire的一点是要与火力地堡保持你的JavaScript数据模型同步在任何时候。你不希望创建一个新的AngularFire承诺每次需要获取数据的时间。你刚才初始化AngularFire一次,本地数据将永远是最新的。

myapp.service('firebaseAuth',['angularFireCollection',函数(angularFireCollection){
    this.firebaseRef =新的火力地堡(https://test.firebaseio.com);
    this.initUserInfo =功能(ID){
        如果(!this.userRef){
            this.userRef = this.firebaseRef.child('/人/'+ id)的;
            this.userInfo = angularFireCollection(this.userRef);
        }
        其他{
            //已初始化
        }
    }
}]);

记住,你的服务的所有属性(即一切你指定使用这个关键词)从与此服务注入控制器访问。所以,你可以做这样的事情的console.log(firebaseAuth.userInfo) firebaseAuth.userRef.on('值',函数(SNAP){。 ..});

此外,你可能最终要使用的 FirebaseAuthClient 为您的用户身份验证。

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

Should I $rootScope the returned user info?

or

Call below code in each controller?

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

UPDATE

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;
  }
});

解决方案

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
        }
    }
}]);

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) { ... });

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

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

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