如何计算angularfire中的子元素? [英] How to count child elements in angularfire?
问题描述
我正在为我的应用程序使用 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;
});
}
});
但这不会创建评论的子项- postId 也没有计数器.请帮助我.我是 firebase 和 angular 的新手,非常感谢您的帮助.
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 专家更容易做出回应(因为最新版本的文档最容易找到).
如果我忽略其他所有内容,这就是您更新帖子评论数量的代码.
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 包装了 transaction
方法.即使是这样,我也不会为此使用 AngularFire.AngularFire 提供了从 Firebase 的常规 JavaScript SDK 到 AngularJS 前端的绑定.计数评论不是前端功能,所以我会坚持使用 JavaScript SDK.显示评论计数是前端功能,因此我会将其绑定到带有 AngularFire 服务的 $scope
.
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.
现在是实际代码:
var countRef = ref.child('comments').child(postId) .child('commentcount');
countRef.transaction(function (count) {
return (count || 0) + 1;
});
请注意,最后一个片段是 transaction
的 Firebase 文档.一个更易读的版本是:
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;
});
这篇关于如何计算angularfire中的子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!