如何仅在从firebase获取所有数据时使用limitTo过滤器

时间:2015-06-22 13:38:28

标签: javascript angularjs firebase angularjs-filter

我想只显示3条最近的评论(每条评论都有“id”值),但limitTo和orderBy过滤器在这里无效。我想这是因为当过滤器运行时,还没有来自firebase的数据。

如何仅在类别时应用这些过滤器。$ loaded()== true?我尝试使用自定义过滤器,但没有设法正确执行...

<div ng-repeat="category in categories">
        <div ng-repeat="vendor in category.vendors">
            <div ng-repeat="review in vendor.reviews | orderBy: -id | limitTo:3">
                <img src="img/placeholder-avatar.jpg" alt="" />
                <div class="right-content">
                    <h4>{{ vendor.name }}</h4>
                    <div class="stars"></div>
                    <span class="category"> {{ category.name }} </span>
                    <p> {{ review.body | limitTo:100 }}{{ review.body.length < 100 ? '' : '...'}} </p>
                </div>
            </div>
        </div>
    </div>

在控制器中我只有:

app.controller('mainCtrl', function($scope, $http, $firebaseObject) {
  var ref = new Firebase("https://scorching-torch-1679.firebaseio.com");
  $scope.categories = $firebaseObject(ref);
});

我的firebase结构(这里我试图进入评论,分配给类别内的供应商):

enter image description here

1 个答案:

答案 0 :(得分:1)

这将有助于在类别之后加载。$ loaded()== true:

 <div ng-if="categories.$loaded()">
     <div ng-repeat="category in categories">
       <div ng-repeat="vendor in category.vendors">
        <div ng-repeat="review in vendor.reviews | orderBy: -id | limitTo:3">
            <img src="img/placeholder-avatar.jpg" alt="" />
            <div class="right-content">
                <h4>{{ vendor.name }}</h4>
                <div class="stars"></div>
                <span class="category"> {{ category.name }} </span>
                <p> {{ review.body | limitTo:100 }}{{ review.body.length < 100 ? '' : '...'}} </p>
            </div>
        </div>
    </div>
</div>

但我不认为有问题。如果你想在类别之后进行过滤。$ loaded()== true,请在你自己的过滤器中进行过滤:

app.filter('myFilter', function(){ 
       function myFilter(elms, filters) {
            if(categories.$loaded()) {
              // do filter stuff..
            } else {
              // return all 
            }
       };
});