Skip to main content

Simple Increment and Decrement Using AngularJS

In this article we will create a program Simple Increment and Decrement using AngularJS. An advance programming technique that use AngularJS framework to develop a voting poll in JavaScript language. You are free to modify and use this code.

Getting Started:

First you have to download Bootstrap, this is the link for the bootstrap that i used for the layout design And, this is the link for the AngularJS

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 your 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="">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">Simple Increment And Decrement Using AngularJS</h3>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <div class="col-md-10">
  20. <div class="alert alert-info">Most Favorite Artis</div>
  21. <table class="table table-bordered">
  22. <thead class="alert-info">
  23. <tr>
  24. <th>Full Name</th>
  25. <th>Count</th>
  26. <th>Action</th>
  27. </tr>
  28. </thead>
  29. <tr ng-repeat="member in members">
  30. <td>{{}}</td>
  31. <td>{{member.Votes}}</td>
  32. <td><button class="btn btn-primary btn-sm" ng-click= "incrementUp(member)"><span class="glyphicon glyphicon-arrow-up"></span> Increment</button> | <button class="btn btn-danger btn-sm" ng-click= "incrementDown(member)"><span class="glyphicon glyphicon-arrow-down"></span> Decrement</button></td>
  33. </tr>
  34. </tbody>
  35. </table>
  36. </div>
  37. </div>
  38. </body>
  39. </html>

Creating the Main Function

This code contains the main function of the application. This code will dynamically count the total vote in the table. 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){
  4. var members =[
  5. {name: "Chris Evans", Votes: 0},
  6. {name: "Emilia Clark", Votes: 0},
  7. {name: "Jason Momoa", Votes: 0},
  8. {name: "Robert Downey Jr.", Votes: 0},
  9. ];
  11. $scope.members = members;
  13. $scope.incrementUp = function(member){
  14. member.Votes++;
  15. }
  16. $scope.incrementDown = function(member){
  17. member.Votes--;
  18. }
  19. });
There you have it we successfully created a Simple Increment and Decrement 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