【发布时间】:2015-01-26 12:10:12
【问题描述】:
有没有办法像这样从 firebase 获取随机记录:
{
"-JbmopNnshefBT2nS2S7" : {
"dislike" : 0,
"like" : 0,
"post" : "First post."
},
"-JbmoudijnJjDBSXNQ8_" : {
"dislike" : 0,
"like" : 0,
"post" : "Second post."
}
}
我用这段代码解决了这个问题,但是它下载了所有的记录,所以如果数据库更大,我的应用程序会运行得很慢:
HTML 代码:
<div ng-controller="RandomCtrl">{{RandomPost.post}}</div>
JS代码:
var app=angular.module('App', ['firebase']);
app.controller('RandomCtrl', function($scope, $firebase){
var ref = new Firebase("https://ind.firebaseio.com/");
var sync=$firebase(ref);
$scope.messages = sync.$asArray();
$scope.GetRandomPost=function(){
return $scope.RandomPost=$scope.messages[Math.floor(Math.random()*$scope.messages.length)];
};
$scope.GetRandomPost();
});
【问题讨论】:
-
您可以使用增量作为键,然后简单地随机选择一个。
标签: database angularjs firebase angularfire