(Ionic - Firebase)为什么只显示最后一个数据?
问题(Ionic - Firebase)为什么只显示最后一个数据?
我使用离子&火力地堡的开发应用。
我不知道为什么,但只显示添加到数据库的最后一个数据。
我试图用“价值”而不是“child_added”,但我有一个列表,只有原始值{“数据”:“你好”}
我想获得一个列表只有价值!
感谢您的帮助
news.html
<ion-view title="News">
<ion-nav-buttons side="right">
<button ng-click="messageCreator()" class="button button-icon ion-ios-chatboxes-outline"></button>
</ion-nav-buttons>
<ion-content class="has-header">
<ion-list ng-controller="displayMessages">
<ion-item ng-click="goCalendar()" ng-repeat="val in value" class="item item-icon-right">
{{val}}
<a class="icon icon-accessory ion-chevron-right"></a>
</ion-item>
</ion-list>
</ion-content>
</ion-view>
方法创建一个新的数据
$scope.messageCreator = function() {
$ionicPopup.prompt({
title: 'New message',
}).then(function(res) {
firebase.database().ref('ParentName').push().set({
data: res
}).then(function() {
alert("Data submitted");
}).catch(function(error) {
alert(error.message);
});
});
}
用于显示数据的方法
$scope.displayMessages = function ($timeout) {
var testRef = firebase.database().ref('ParentName');
testRef.on('child_added', function (snapshot) {
$timeout(function() {
update(snapshot);
});
});
function update (snapshot) {
$scope.value = snapshot.val();
}
}
只显示最后的数据添加到数据库
我不认为有任何理由:
testRef.on('child_added', function (snapshot) {
$timeout(function() {
update(snapshot);
});
});
的问题是类型的数据。您应该将数据声明为Observable,以便即时刷新,而无需额外的代码。该声明是这样的:
import {AngularFireDatabase, FirebaseListObservable} from 'angularfire2/database';
可以检查准备教程here,这也提供了一个活生生的例子来测试它:
data: FirebaseListObservable<any[]>;
此类型可以通过被包括在内。
这是在TypeScript中,不... ...? 如何在JavaScript中做同样的事情? –
是的,这是打字稿。为什么你需要Javascript,如果你正在使用Ionic打字稿是默认的。导入在Javascript(ES6)中是相同的。你可以用Js声明数据,如:var data = FirebaseListObservable []; 。我不确定这是否会奏效。但首先你应该使用Ionic推荐的打字稿 – Temeteron
其实,感谢你,我开始学习TypeScript和我重新启动了我的项目......它会更好^^ –
到列表中... –