我使用angularfire我的应用程序我在这里列举我的服务和控制器。我计划得到已经添加到特定帖子的评论数。这是我的控制器
Hi I am using angularfire for my app I am listing my service and controller here. I am planning to get the count of comments which has been added to a particular post. this is my controller
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事件这样的计数器试图
I have tried using transaction to update a counter on the addComment event like this
$scope.comments.$add(comment, function(error) {
if (!error) {
$firebase(ref.child('comments').child(postId)
.child('commentcount')).transaction(function (count) {
return count + 1;
});
}
});
但是,这并不创建一个孩子comments--帖子ID也没有一个计数器。请你帮帮我。我是新来的火力点和角度,你的帮助将非常AP preciated。
but this does not create a child to comments-- postId nor is there a counter. Please do help me. I am new to firebase and angular and your help will be much appreciated.
的您似乎使用AngularFire的旧版本。请更新到最新版本。虽然它可能不会使你张贴问题的差异,这将使它更容易为AngularFire专家回应(因为最新版本的文档是最容易找到)。的
如果我无视一切,这是code,你要更新评论数的计数的一个职位。
If I ignore everything else, this is the code where you're updating the count of the number of comments for a post.
$firebase(ref.child('comments').child(postId)
.child('commentcount')).transaction(function (count) {
return count + 1;
});
我不认为AngularFire包装了交易
方法。即使是这样,我不会用AngularFire这一点。 AngularFire提供了一个从火力地堡的常规的JavaScript SDK到AngularJS前端绑定。计数的意见是不是前端功能,所以我只是坚持给JavaScript SDK为。的显示的评论数为前端功能,所以我将绑定到 $范围
与AngularFire服务。
I don't think AngularFire wraps the transaction
method. And even if it does, I would not use AngularFire for this. AngularFire provides a binding from Firebase's regular JavaScript SDK to the AngularJS front-end. Counting comments is not front-end functionality, so I would just stick to the JavaScript SDK for that. Displaying the comment count is front-end functionality, so I would bind that to the $scope
with AngularFire services.
现在实际code:
var countRef = ref.child('comments').child(postId) .child('commentcount');
countRef.transaction(function (count) {
return (count || 0) + 1;
});
请注意,这最后的片段是从的为交易
火力地堡文档。稍微更可读的版本是:
Note that this last snippet is a pretty literal copy from the Firebase documentation for transaction
. A slightly more readable version is:
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;
});