2015-07-11 67 views
0

嗨我正在使用angularfire为我的应用程序 我在这里列出我的服务和控制器。我计划获得已添加到特定职位的评论数。这是我的控制器如何计算angularfire中的子元素?

app.controller('PostViewCtrl', function ($scope,       
    FIREBASE_URL,$routeParams, 
    Post, Auth, $timeout,$interval,$http, $rootScope, $firebase) { 

    var ref = new Firebase(FIREBASE_URL); 
    $scope.post = Post.get($routeParams.postId); 
    $scope.comments = Post.comments($routeParams.postId); 


     $scope.user = Auth.user; 
     $scope.signedIn = Auth.signedIn; 


      $scope.addComment = function() { 
     if(!$scope.commentText || $scope.commentText === '') { 
     return; 
      } 
      var Commentcreatedtime = moment().format('llll'); 
      $scope.CommentcreatedTime = Commentcreatedtime; 
     var comment = { 
    createdTime: $scope.CommentcreatedTime, 
    text: $scope.commentText, 
    creator: $scope.user.profile.username, 
    creatorUID: $scope.user.uid, 
    creatorpic: $scope.user.profile.userpic, 
    commentimage: $scope.object.image.info.uuid 
}; 


$scope.comments.$add(comment); 




$scope.commentText = ''; 
$scope.object.image.info.uuid = ''; 
    }; 

    }); 

这是我的服务

'use strict'; 

    app.factory('Post', function($firebase,FIREBASE_URL){ 

    var ref = new Firebase(FIREBASE_URL); 
    var posts = $firebase(ref.child('posts')).$asArray(); 

    var Post = { 
    all: posts, 
    create: function(post){ 
    return posts.$add(post).then(function(postRef){ 
     $firebase(ref.child('user_posts').child(post.creatorUID)) 
        .$push(postRef.name()); 
     return postRef; 
    }); 
     }, 
    get: function(postId){ 
    return $firebase(ref.child('posts').child(postId)).$asObject(); 

    }, 
     delete: function(post){ 
    return posts.$remove(post); 
     }, 
    comments: function(postId){ 

    return $firebase(ref.child('comments').child(postId)).$asArray(); 
     } 
     }; 

     return Post; 

     }); 

我已经使用事务以更新addComment事件这样

$scope.comments.$add(comment, function(error) { 
    if (!error) { 
    $firebase(ref.child('comments').child(postId) 
    .child('commentcount')).transaction(function (count) { 
     return count + 1; 
    }); 
    } 
    }); 

计数器尝试,但这种不创建孩子评论 - postId也没有柜台。请帮助我。我是新来的firebase和角度和你的帮助将不胜感激。

回答

0

您似乎正在使用旧版本的AngularFire。请更新至最新版本。虽然它可能对你发布的问题没有什么影响,但它会让AngularFire专家更容易回应(因为最新版本的文档最容易找到)。

如果我忽略其他所有内容,那么这是代码,您要更新帖子的评论数。

$firebase(ref.child('comments').child(postId) 
.child('commentcount')).transaction(function (count) { 
    return count + 1; 
}); 

我不认为AngularFire包装transaction方法。即使这样做,我也不会使用AngularFire。 AngularFire提供了从Firebase的常规JavaScript SDK到AngularJS前端的绑定。对评论进行计数并不是前端功能,因此我只需坚持使用JavaScript SDK即可。 显示评论数量是前端功能,所以我会将它绑定到使用AngularFire服务的$scope

现在的实际代码:

var countRef = ref.child('comments').child(postId) .child('commentcount'); 
countRef.transaction(function (count) { 
    return (count || 0) + 1; 
}); 

注意,这最后的片段是从Firebase documentation for transaction一个漂亮的文字拷贝。稍微更可读的版本是:

var countRef = ref.child('comments').child(postId) .child('commentcount'); 
countRef.transaction(function (count) { 
    if (!count) { 
    // if count does not exist, we apparently don't have any comments yet 
    count = 0; 
    } 
    count = count + 1; 
    return count; 
});