Angularjs 如何计算angularfire中的子元素?
嗨,我正在为我的应用程序使用angularfire 我在这里列出我的服务和控制器。我正计划获得添加到特定帖子的评论数。这是我的控制器Angularjs 如何计算angularfire中的子元素?,angularjs,firebase,angularfire,Angularjs,Firebase,Angularfire,嗨,我正在为我的应用程序使用angularfire 我在这里列出我的服务和控制器。我正计划获得添加到特定帖子的评论数。这是我的控制器 app.controller('PostViewCtrl', function ($scope, FIREBASE_URL,$routeParams, Post, Auth, $timeout,$interval,$http, $rootScope, $firebase) {
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和angular的新手,非常感谢您的帮助 您似乎正在使用AngularFire的旧版本。请更新至最新版本。虽然这可能不会对您发布的问题产生影响,但会使AngularFire专家更容易做出响应(因为最新版本的文档最容易找到)
如果我忽略了其他所有内容,这就是更新帖子评论数的代码
$firebase(ref.child('comments').child(postId)
.child('commentcount')).transaction(function (count) {
return count + 1;
});
我不认为AngularFire包装了事务
方法。即使是这样,我也不会用AngularFire来做这个。AngularFire提供了从Firebase的常规JavaScript SDK到AngularJS前端的绑定。计算评论不是前端功能,因此我会坚持使用JavaScript SDK。显示注释计数是前端功能,因此我将使用AngularFire services将其绑定到$scope
现在了解实际代码:
var countRef = ref.child('comments').child(postId) .child('commentcount');
countRef.transaction(function (count) {
return (count || 0) + 1;
});
请注意,最后一个代码段是。更具可读性的版本是:
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;
});