如何将从Firebase检索到的数据显示到AngularJS App

时间:2017-07-11 11:57:08

标签: angularjs firebase firebase-realtime-database angularjs-scope angularjs-view

我有一个从firebase接收数据的角度应用程序,但我不知道如何在视图中显示数据。从firebase检索的数据是一个具有3个具有以下名称的对象的对象:KoAJWEHt6E2krjLgSuB,KoAJWEPc0seRt95JqoH,KoAJWEQDiAsOWwvL2QZ(这些名称是firebase分配的唯一键)。这3个对象保存每个人的分类数据。现在我不知道如何在视图中显示这些数据。我已经尝试过ng-repeat但它没有显示任何内容,并且控制台中没有错误。

控制器:

myApp.controller("classifiedsCtrl",["$scope","$http","$mdSidenav","$mdToast","$mdDialog","$firebaseArray",function($scope,$http,$mdSidenav,$mdToast,$mdDialog,$firebaseArray){
      var ref=firebase.database().ref().child('persons');
      ref.on('value',function(snapshot){
      $scope.classifieds=snapshot.val();
      console.log($scope.classifieds);
   });
}]);

控制器视图:

<md-card flex="30" ng-repeat="classified in classifieds | filter:classifiedsFilter" class="record">
 <md-card-content>
    <div class="classified-info" ng-show="!showContact">
        <h2 class="md-title">{{classified.title | uppercase}}</h2>
        <h3>{{classified.price | currency}}</h3>
        <p>{{classified.description}}</p>
    </div>
    <div class="classified-contact" ng-show="showContact">
        <p>
          <md-icon class="mdi mdi-account"></md-icon>{{classified.name}}</p>
        <p>
          <md-icon class="mdi mdi-phone"></md-icon>{{classified.phone}}</p>
    </div>
     <div layout="row">
           <md-button ng-click="showContact=true" ng-show="!showContact">Contact</md-button>
           <md-button ng-click="showContact=false" ng-show="showContact">Details</md-button>
           <md-button ng-click="showAdmin=true" ng-show="!showAdmin">Admin</md-button>
           <md-button ng-click="showAdmin=false" ng-show="showAdmin">Close</md-button>
     </div>
     <div class="classified-admin" ng-if="showAdmin">
          <h3>Admin</h3>
          <div layout="row">
              <md-button class="md-raised md-primary">Edit</md-button>
                    <md-button ng-click="deleteClassified($event,classified)" class="md-raised md-warn">Delete</md-button>
                </div>
            </div>
        </md-card-content>
    </md-card>

1 个答案:

答案 0 :(得分:0)

将你的firebase引用传递给firebasearray,如下所示

var ref=firebase.database().ref().child('persons'); 
$scope.ref=$firebaseArray(ref);
$scope.ref.$loaded().then(function(){
$scope.classifieds=[];
$scope.classifieds=$scope.ref;    

})

然后等待ref加载将条件放在html中,如下所示

<div ng-show="classifieds.length>0">
//put your md-card inside this div.
</div>