该错误未在开发人员工具中显示,因此我想它可能与数据本身以及其读取方式有关。 {{upVote}}和{{downVote}}都没有值,点击时显示为空。按钮以某种方式链接在一起?我正在为每个项目设置每个投票。
背景
投票系统,具有独立的上下得分(未计入单个投票得分)。
希望分数保留在数据库中。
我没有考虑过限制每位用户的投票,但是如果您对此有任何想法,请随时在您的回复中加入。谢谢!
JS文件
$scope.upVote = function () {
if ($scope.voteType == "down") {
$scope.upVote++;
}
$scope.upVote++;
$scope.voteType = "up";
};
$scope.downVote = function () {
if ($scope.voteType == "up") {
$scope.downVote++;
}
$scope.downVote++;
$scope.voteType = "down";
};
帖子在$ scope.post中保存为:
$scope.post = {
title: '',
upVote: 0,
downVote: 0
};
该按钮在html中是这样的:
<i ng-click="downVote()"
class="fa fa-chevron-circle-down fa-2x"
ng-class="{true:'downVote', false:''}[vote=='downVote']"></i>
最佳答案
$scope
在控制器范围内相同。它在upVote
中不变。
angular.module('starter').controller('PostCtrl', function($scope, Post) {
$scope.posts = Post.all;
$scope.upVote = function () {
$scope.upVote++; // NOT the upVote property of the clicked post
...
};
});
您需要像这样从
$scope.posts
获取帖子:angular.module('starter').controller('PostCtrl', function($scope, Post) {
$scope.posts = Post.all;
$scope.upVote = function (post) {
post.upVote++;
...
};
});
在ng-repeat中传递
post
:<div class="row" ng-repeat="(postId, post) in posts">
<i ng-click="upVote(post)" ...></i>
...
</div>
这样,您将引用单击的帖子,而不是
$scope
本身的属性。关于javascript - JS投票系统{{bindings}}的点击是否无效?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27241748/