如何获得帖子的评论数量

时间:2015-10-16 20:05:38

标签: angularjs firebase

我正在尝试获取帖子的评论数量,但无法找到方法。如果你想检查的话,我有一个插件。

<div class="anuncios">
    <div class="panel panel-default" ng-repeat="posts in post | orderBy:'-FechaAnuncio'">
      <div class="panel-heading" style="overflow:hidden;">
        <p><a href="#">{{posts.NombreLocal}}</a></p>
      </div>
      <div class="panel-body">
        <p>{{posts.Anuncio}}</p>
        <hr>
        <small>{{posts.FechaAnuncio | date:'HH:mm:ss dd-MM-yyyy'}}</small>
        <button class="btn btn-primary" style="float:right;" ng-click="showme = !showme"><span ng-hide="showme">Show Comments: {{posts.comments.length}}</span><span ng-show="showme">Hide Comments</span></button>
        <div>
          <div class="well" ng-show="showme">
            <div class="input-group">
              <input type="text" class="form-control" placeholder="Comment Here..." ng-model="comment.text">
              <span class="input-group-btn">
                <button class="btn btn-default" type="button" ng-click="addComment(posts, comment)">Publish!</button>
                </span>
            </div>
            <!-- /input-group -->
            <div class="cmt">
              <div class="panel" ng-repeat="comments in posts.comments">
                <p>{{comments.NombreLocal}}</p>
                <p>{{comments.FechaComentario | date:'HH:mm:ss dd-MM-yyyy'}}</p>
                <hr>
                <p>{{comments.Comment}}</p>
                <p></p>
              </div>
              <!--FIN DE COMENTARIOS -->
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

http://plnkr.co/edit/AIx0x7?p=preview

评论数量应显示在“显示评论:”

旁边

这是我的Firebase结构: Firebase structure image

2 个答案:

答案 0 :(得分:2)

由于comments是一个对象,最简单的方法是计算它拥有的键数。您需要将其添加到控制器中,或者您可以创建自己的过滤器。

app.controller('AnunciosCtrl', function($scope, $firebaseArray, $timeout) {
  ...
  $scope.sizeOf = function(obj) {
    return Object.keys(obj || {}).length;
  };
});

HTML:

<span ng-hide="showme">Show Comments: {{ sizeOf(posts.comments) }}</span>

Plunker

答案 1 :(得分:-3)

在您的控制器中执行:

$scope.numComments = $scope.post.comments.length;

在你的HTML中:

...>Show Comments: {{numComments}}<...