【问题标题】:Firebase data not loading in list unless refreshed (angularJS, firebase & ionic)除非刷新,否则 Firebase 数据不会加载到列表中(angularJS、firebase 和 ionic)
【发布时间】:2016-06-30 13:33:51
【问题描述】:

我的项目列表未正确加载。我得到一个没有错误的空屏幕,一旦我刷新,它们都会显示出来。我相信 firebase 数据的检索速度不够快,但我不知道该怎么做。我尝试了多种方法,但没有运气。提前谢谢!

app.controller('SpotsCtrl', function($state, $scope, $stateParams) {

var firebaseRef = new Firebase("https://myBrokenApp.firebaseio.com/spots");

firebaseRef.child('0').on('value', function(dataSnapshot){
    console.log("hello");

    $scope.spots = dataSnapshot.val();
    console.log($scope.spots);
})

});

//HTML

<ion-list show-delete="spot.showDelete" show-reorder="spot.showMove"> 

 <ion-item
     class="item-avatar-left item-icon-right item-remove-animate" 
       ng-repeat="spot in spots"
       href="#/tab/spots/{{spot.name}}">

      <h2>{{ spot.name }}</h2>
      <p>{{ spot.title }}</p>
      <p>{{ spot.loc }}</p>
      <i class="icon ion-ios-clock-outline "></i>

       <ion-delete-button ng-click="deleteSpot($index)" 
                        class="ion-minus-circled">
       </ion-delete-button>

      <ion-reorder-button class="ion-navicon"
       on-reorder="moveSpot(spot, $fromIndex, $toIndex)">

      </ion-reorder-button>
 </ion-item>
</ion-list >

【问题讨论】:

    标签: javascript angularjs angularjs-directive firebase


    【解决方案1】:

    尝试将您的 firebaseref 绑定到 $firebaseObject。

    app.controller('SpotsCtrl', function($state, $scope, $stateParams, $firebaseObject) {
    
      var firebaseRef = new Firebase("https://myBrokenApp.firebaseio.com/spots");
      $scope.spots = {};
      var spots = $firebaseObject(firebaseRef.child('0'));
    
      spots.$loaded().then(function() {
        $scope.spots = spots;
        console.log($scope.spots);
      });
    });
    

    【讨论】:

    • 感谢您的回复,没有运气:(还有其他想法吗??
    • 嗯,好吧.. 尝试将 firebaseRef.child('0').on('value', function(dataSnapshot){... 包装在 $timeout(function(){ firebaseRef.child... }); 中
    • nope :/ 一旦屏幕加载,如果数据还没有准备好,它就会一直保持空白,直到我刷新
    • 试试我最初的答案,但添加$scope.spots = {};。 console.log() 没有在 $loaded.then-function 中写任何东西吗?
    • 成功了!!啊啊啊太感谢了! ..我明白了,为什么这样有效?
    猜你喜欢
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 2018-08-21
    • 2021-05-14
    • 1970-01-01
    • 2013-07-17
    相关资源
    最近更新 更多