我試圖從Firebase獲取數據。我的做法是更好地學習Firebase,我的應用就像一個消息應用。Angularjs Firebase獲取數據列表
我的問題是,我試圖獲取這些消息數據,但因爲他們有一個唯一的ID,我無法聯繫到他們。我怎樣才能達到messagecontents
,dates
?
這裏是我的數據庫結構:
angularish
-> <id>
-> "sender" : "john"
-> "to" : "sarah"
-> "date" : "16/09/2016"
-> "messageContent" : "Something like a long text..."
-> <id>
-> "sender" : "john"
-> "to" : "sarah"
-> "date" : "16/09/2016"
-> "messageContent" : "Something like a long text..."
-> <id>
-> "sender" : "john"
-> "to" : "sarah"
-> "date" : "16/09/2016"
-> "messageContent" : "Something like a long text..."
這裏是我的代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test3 Angular</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.4.1/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.4.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.4.0/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.4.0/firebase-database.js"></script>
<!-- Leave out Storage -->
<!-- <script src="https://www.gstatic.com/firebasejs/3.4.0/firebase-storage.js"></script> -->
<script>
// Initialize Firebase
firebase.initializeApp(config);
</script>
<script>
var app2 = angular.module("theapp",[]);
app2.controller("generalController",function($scope){
var database = firebase.database();
var rootRef = database.ref();
var subRootRef = rootRef.child("angularish");
$scope.messages = [];
rootRef.push({
"sender":"y",
"to" : "x",
"messageContent" : "Something like a long text",
"date" : "10/06/2016"
});
rootRef.on("value",function(snapshot){
$scope.messages = snapshot.val();
console.log(snapshot.val());
});
});
</script>
</head>
<body>
<div class="container" ng-app="theapp" ng-controller="generalController">
{{ 1+1 }}
<ul>
<li ng-repeat="message in messages">
{{ message.messageContent }}
</li>
</ul>
</div>
</body>
</html>
如果使用angularFire – charlietfl
我試圖使用它,但我又不能:(我怎麼能做到這一點在angularfire –
您可以使用'startAt'和'endAt' HTTPS非常非常簡單://火力.google.com/docs/database/web/retrieve-data#filtering_data 繼承人一個示例http://jsfiddle.net/katowulf/vQEmt/2/ –