JavaScript - Simple Upvote and Downvote Count Using AngularJS

In this tutorial we will create a Simple Upvote and Downvote Count Using AngularJS. This code will count the total votes to each person every time the button is click. The code itself use AngularJS buil-in function incrementUp() and incrementDown() to count the general majority of votes every time the user click the button. This a user-friendly program feel free to modify and use it to your system. We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.

Getting Started:

First you have to download Bootstrap, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/. And, this is the link for the AngularJS https://angularjs.org/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it index.html.
  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="myModule">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width-device=width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6.                 <script src="js/angular.js"></script>
  7.                 <script src="js/script.js"></script>
  8.         </head>
  9. <body ng-controller="myController">
  10.         <nav class="navbar navbar-default">
  11.                 <div class="container-fluid">
  12.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  13.                 </div>
  14.         </nav>
  15.         <div class="col-md-3"></div>
  16.         <div class="col-md-6 well">
  17.                 <h3 class="text-primary">JavaScript - Simple Upvote and Downvote Count Using AngularJS</h3>
  18.                 <hr style="border-top:1px dotted #000;"/>
  19.                 <table class="table table-bordered">
  20.                         <thead class="alert-info">
  21.                                 <tr>
  22.                                         <th>Full Name</th>
  23.                                         <th>Action</th>
  24.                                         <th>Vote Count</th>
  25.                                 </tr>
  26.                         </thead>
  27.                         <tbody>
  28.                                 <tr ng-repeat="member in members">
  29.                                         <td>{{member.name}}</td>
  30.                                         <td><button class="btn btn-primary btn-sm" ng-click= "incrementUp(member)"><span class="glyphicon glyphicon-arrow-up"></span> Upvote</button> | <button class="btn btn-danger btn-sm" ng-click= "incrementDown(member)"><span class="glyphicon glyphicon-arrow-down"></span> Downvote</button></td>
  31.                                         <td>{{member.Votes}}</td>
  32.                                 </tr>
  33.                         </tbody>
  34.                 </table>
  35.         </div>
  36. </body>        
  37. </html>

Creating the Main Function

This code contains the main function of the application. This code will count the votes when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory.
  1. var app = angular.module("myModule", [])
  2.                 .controller("myController" , function($scope){
  3.                                                                        
  4.         var members =[
  5.                 {name: "John Smith", Votes: 0},
  6.                 {name: "Claire Temple", Votes: 0},
  7.         ];                                     
  8.  
  9.         $scope.members = members;
  10.        
  11.         $scope.incrementUp = function(member){
  12.                 member.Votes++;
  13.         }
  14.         $scope.incrementDown = function(member){
  15.                 member.Votes--;
  16.         }
  17. });    
There you have it we successfully created a Simple Upvote and Downvote Count using AngularJS. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment