gpt4 book ai didi

javascript - 在 Angular 应用程序中为每个帖子投票一次

转载 作者:行者123 更新时间:2023-11-29 19:13:12 25 4
gpt4 key购买 nike

目前,用户通常只能投赞成票或反对票一次。我希望用户能够对每个帖子投赞成票或反对票。

<div ng-repeat="post in posts | orderBy:'-upvotes'">

<span class="glyphicon glyphicon-thumbs-up"
ng-click="incrementUpvotes(post)" ng-style="post.hadUpvoted ? {color: 'red'} : {}"></span>
{{post.upvotes}}

<span class="glyphicon glyphicon-thumbs-down" ng-click="downvote(post)"
ng-style="post.hadDownvoted ? {color:'red'} : {}"></span>

Controller :

var upvoted;
$scope.incrementUpvotes = function(post) {
if(!upvoted) {
posts.upvote(post);
upvoted = true;
post.hadUpvoted = true;
}
};

服务:

o.upvoteComment = function(post, comment) {
return $http.put('/posts/' + post._id + '/comments/' + comment._id + '/upvote', null, {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function(data) {
comment.upvotes += 1;
});
};

最佳答案

不是检查全局 upvoted 变量,而是针对每个帖子检查它。

你的 Controller 应该是这样的,

$scope.incrementUpvotes = function(post) {
if(!post.hadUpvoted) {
posts.upvote(post);
post.hadUpvoted = true;
}
};

想法是为每个帖子在本地设置 upvoted 变量。

希望对您有所帮助。如有任何疑问,请在评论中提问。

关于javascript - 在 Angular 应用程序中为每个帖子投票一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37259730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com