Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Handling events in AngularJS

Suggested Videos
Part 4 - AngularJS ng-src directive
Part 5 - Two way databinding in AngularJS
Part 6 - AngularJS ng-repeat directive

In this video we will discuss how to handle events in AngularJS.

Let us understand with an example. Here is what we want to do.

Handling events in AngularJS
1. Display the list of technologies in a table
2. Provide the ability to like and dislike a technology
3. Increment the likes and dislikes when the respective buttons are clicked

Script.js : In the controller function we have 2 methods to increment likes and dislikes. Both the functions have the technology object that we want to like or dislike as a parameter. 

var app = angular
            .module("myModule", [])
            .controller("myController", function ($scope) {

                var technologies = [
                    { name: "C#", likes: 0, dislikes: 0 },
                    { name: "ASP.NET", likes: 0, dislikes: 0 },
                    { name: "SQL", likes: 0, dislikes: 0 },
                    { name: "AngularJS", likes: 0, dislikes: 0 }

                $scope.technologies = technologies;

                $scope.incrementLikes = function (technology) {

                $scope.incrementDislikes = function (technology) {

HtmlPage1.html : Notice in the html below, we are associating incrementLikes() and incrementDislikes() functions with the respective button. When any of these buttons are clicked, the corresponsing technology object is automatically passed to the function, and the likes or dislikes property is incremented depending on which button is clicked.

<!DOCTYPE html>
<html xmlns="">
    <script src="Scripts/angular.min.js"></script>
    <script src="Scripts/Script.js"></script>
    <link href="Styles.css" rel="stylesheet" />
<body ng-app="myModule">
    <div ng-controller="myController">
                <tr ng-repeat="technology in technologies">
                    <td> {{ }} </td>
                    <td style="text-align:center"> {{ technology.likes }} </td>
                    <td style="text-align:center"> {{ technology.dislikes }} </td>
                        <input type="button" ng-click="incrementLikes(technology)" value="Like" />
                        <input type="button" ng-click="incrementDislikes(technology)" value="Dislike" />

Styles.css : Styles for table, td and th elements

table {
    border-collapse: collapse;

td {
    border: 1px solid black;
    padding: 5px;

th {
    border: 1px solid black;
    padding: 5px;
    text-align: left;

AngularJS tutorial for beginners

1 comment:

  1. Thanks venkat....
    All your tutorials are very very good its easy to understand and quickly adaptable.


If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.