(Ionic - Firebase)为什么只显示最后一个数据?

时间:2017-04-26 11:39:54

标签: javascript firebase ionic-framework firebase-realtime-database

问题

我使用Ionic& amp; Firebase用于开发应用程序。

我不知道为什么,但只显示添加到数据库的最后数据。

我试图使用"价值"而不是" child_added"但是我得到的列表只包含原始值{"数据":" Hello"}

我想获得一个只包含值的列表!

感谢您的帮助

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

仅显示添加到数据库的最后数据

Only last data added

1 个答案:

答案 0 :(得分:0)

我认为没有任何理由:

Data-Driven Triggers

问题是数据的类型。您应该将数据声明为Observable,以便即时刷新而无需额外的代码。声明是这样的:

testRef.on('child_added', function (snapshot) {
  $timeout(function () {
    update(snapshot);  
  });
});

此类型可包括在内:

data: FirebaseListObservable<any[]>;

您可以查看准备好的教程here,它还提供了一个实例来测试它。