Hey guys may i know how can i create a up/down voting function in angularjs ? i want to make it to something similar to stackoverflow or reddit. i found a Jquery plugin https:/
Just use a single scope variable to act like a toggle button.
Sample Demo: http://plnkr.co/edit/C4w9hDK3xW1R0ua3WPgU?p=preview
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}}
Controller Logic:
app.controller('MainCtrl', function($scope) {
$scope.changeVote = function(vote, flag) {
$scope.vote = vote == flag ? 'None' : flag;
alert($scope.vote);
};
});
It should be simple to make something like this.
Here's a simple example:
JS:
var app = angular.module('voteApp', []);
app.controller('MainCtrl', function ($scope) {
$scope.upVote = function () {
$scope.vote++;
}
$scope.downVote = function () {
$scope.vote--;
}
$scope.vote = 0;
});
Fiddle.