从Firebase中选择随机记录

时间:2018-01-09 10:17:19

标签: javascript angularjs firebase firebase-realtime-database angularfire

我有以下代码,我需要选择随机表单对象,我的想法是在html中显示结果,当人重新加载页面时,我可以看到另一个不同的结果。

{
  "-L2LfbYxbwXDXiWNpt27" : {
    "count" : 18
  },
  "-L2PFNOQI28O_F_3YyuK" : {
    "count" : 13
  },
  "-L2PG848o7AGzKZTBnem" : {
    "count" : 17
  }
}



// Initialize Firebase
var config = {
    databaseURL: "https://visit-hotels.firebaseio.com",
};
firebase.initializeApp(config);
var app = angular.module("nowtimereal", ["firebase"]);

// this factory returns a synchronized array of nowusers
app.factory("peopleNow", ["$firebaseArray",
  function($firebaseArray) {
    // create a reference to the database location where we will store our data
    var ref = firebase.database().ref('nowusers');

    // this uses AngularFire to create the synchronized array
    return $firebaseArray(ref);
  }
]);

app.controller("NowCtrl", ["$scope", "peopleNow", function($scope, nowPeople) {
   $scope.now = nowPeople;
   $scope.now.$loaded(
        function(x) {
            $scope.oneResult = x[0];
            console.log($scope.oneResult)
    });

}]);

0 个答案:

没有答案