【问题标题】:How can I get all objects in Firebase with using AngularJs如何使用 AngularJs 获取 Firebase 中的所有对象
【发布时间】:2017-03-19 10:23:10
【问题描述】:

我想从我的 firebase 数据库中获取所有对象,但出现错误。到目前为止,这是我尝试过的方法;

我的 Firebase 数据库(Json);

{
"posts" : {
"-KVWh7aWrTG04ZdtbTKD" : {
  "postText" : "heo1"
},
"-KVWhAC2SkRC_fY75a2I" : {
  "postText" : "heo2"
}
}

这是我的控制器;

$scope.loadPost = function() {
$scope.posts=$firebaseArray(fireBaseData.refpost());
}

还有我的服务;

.factory('fireBaseData', function($firebase) {
 var refPost = new Firebase("https://myapp.firebaseio.com/posts");
 refPost: function() {
  return refPost;
 }
}
})

结果:错误:fireBaseData.refpost 不是函数

任何建议有什么问题?谢谢

【问题讨论】:

    标签: angularjs arrays object firebase firebase-realtime-database


    【解决方案1】:

    总是更喜欢文档而不是教程/博客/网站...

    https://firebase.google.com/docs/database/web/start

    首先,您需要在控制台中使用代码 sn-p 初始化 Firebase SDK。

    然后你为'posts'节点的父亲创建一个引用,像这样。

          var Fatherposts = firebase.database().ref().child('posts');
            Fatherposts.on('value', function(snapshot){
    
              //Finally you get the 'posts' node and send to scope
              $scope.Fatherposts = snapshot.val().posts;
    
            });
    

    转到文档并阅读有关“on”和“once”功能的信息 https://firebase.google.com/docs/database/web/read-and-write

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-20
      • 1970-01-01
      • 2017-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      相关资源
      最近更新 更多