Bootstrap FreeKB - Angular JS - Create a button
Angular JS - Create a button

Updated:   |  Angular JS articles

In this example, the Angular controller will pass item to the View, and then the buttons in the view will increments the values.



The following Controller will store the members of the Simpsons family in a variable named myVariable. There are also two increment functions, to increment likes and dislikes.

var myApp = angular
	.module("myModule", [])
	.controller("myController", function ($scope) {
	var FamilyMembers = [
		{name: "Bart Simpson", likes: 0, dislikes: 0},	
		{name: "Lisa Simpson", likes: 0, dislikes: 0},	
		{name: "Homer Simpson", likes: 0, dislikes: 0},	
		{name: "Marge Simpson", likes: 0, dislikes: 0},			
	$scope.myVariable = FamilyMembers;
	$scope.incrementLikes = function(Simpsons) {
		Simpsons.likes ++;
	$scope.incrementDislikes = function(Simpsons) {
		Simpsons.dislikes ++;



In the view, the following markup can be used to create a table with buttons to increment likes or dislikes. The ng-click directive is used to add buttons that increment likes and dislikes.

    <script src="js/angular.min.js"></script>
    <script src="js/myAngular.js"></script>
<body ng-app="myModule">
    <div ng-controller="myController">
	<table class="table-f6f6f6">
	  <tr ng-repeat="Characters in myVariable">
	    <td>{{ }}</td>	
	    <td>{{ Characters.likes }}</td>
	    <td>{{ Characters.dislikes }}</td>
	      <input type="button" value="Like" ng-click="incrementLikes(Characters)">
	      <input type="button" value="Dislike" ng-click="incrementDislikes(Characters)">


Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee


Add a Comment

Please enter 31e7f3 in the box below so that we can be sure you are a human.