简体   繁体   中英

How to display data retrieved from Firebase to AngularJS App

I have an angular application that is receiving data from firebase but I don't know how to display the data in the view. The data retrieved from firebase is an Object having 3 Objects with these names: KoAJWEHt6E2krjLgSuB,KoAJWEPc0seRt95JqoH,KoAJWEQDiAsOWwvL2QZ (these names are the unique keys assigned by firebase). These 3 objects hold data for classifieds of each person. Now I don't know how to display this data in the view.I have already tried ng-repeat but it doesn't show anything and there is no error in the console.

Controller:

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

Controller View:

<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>

Pass your firebase reference into firebasearray like below

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

})

and then wait for the ref to load put a condition in html like below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM