User @guru's idea: each user can shift vote (up or down) rather than total clicks++/--.
how to create up/down voting function in angularjs
The problem I faced is this one-off vote feature once voted, other users can only change vote (but not add or deduct vote score, i.e -1<= vote score <=1)
My structure is:
- each topic --> one user writes it but many users can vote
- more importantly, up and down votes are separate scores and not netted off
- each user can vote only once but can be changed anytime (i.e up->down, or down->up)
How do I implement so that its a one-off vote system + each user can add votes separately and total votes summed?
guru's version
html
<body ng-controller="MainCtrl">
<i title="Up Votes" ng-click="changeVote(vote, 'up')" class="fa fa-arrow-circle-up fa-2x" ng-class="{true:'up', false:''}[vote=='up']"></i>
<br>
<i title="Down Votes" ng-click="changeVote(vote, 'down')" class="fa fa-arrow-circle-down fa-2x" ng-class="{true:'down', false:''}[vote=='down']"></i>
<br>Vote: {{vote}}
Js
app.controller('MainCtrl', function($scope) {
$scope.changeVote = function(vote, flag) {
$scope.vote = vote == flag ? 'None' : flag;
alert($scope.vote);
};
});
vote increment feature (old version that Im trying to change)
This version adds or remove 1 vote but a user can click multiple times. Other than using Firebase security features, I was trying to make it such that each user can only vote once. (But yet they can change their vote anytime)
var app = angular.module('voteApp', []);
app.controller('MainCtrl', function ($scope) {
$scope.upVote = function () {
$scope.vote++;
}
$scope.downVote = function () {
$scope.vote--;
}
$scope.vote = 0;
});