问题
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 = '';
};
});
this is my service
'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;
});
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;
});
}
});
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.
回答1:
You seem to be using an older version of AngularFire. Please update to the latest version. While it probably doesn't make a difference for the problem you're posting, it will make it easier for AngularFire experts to respond (since the documentation for the latest version is easiest to find).
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;
});
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.
Now for the actual 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;
});
来源:https://stackoverflow.com/questions/31361993/how-to-count-child-elements-in-angularfire