AngularJS
AngularJS Table: Sort, OrderBy & Uppercase Filter [Examples]
Tables are one of the common elements used in HTML when working with web pages. Tables in HTML are...
AngularJS Events are the functionalities that allow web applications to interact with user inputs like mouse click, keyboard inputs, mouse hover, etc. Events need to be handled in web-based applications in order to perform specific tasks and actions. It is triggered when a specific action is performed by the user.
When creating web-based applications, sooner or later your application will need to handle DOM events like mouse clicks, moves, keyboard presses, change events, etc.
AngularJS can add functionality which can be used to handle such events.
For example, if there is a button on the page and you want to process something when the button is clicked, we can use the ng-click event directive.
We will look into Event directives in detail during this course.
In this AngularJS tutorial, you will learn-
The "ng-click directive" in AngularJS is used to apply custom behavior when an element in HTML is clicked. This directive is normally used for buttons because that is the most commonplace for adding events that respond to clicks performed by the user. It is also used to popup alerts when a button is clicked.
<element ng-click="expression"> </element>
Let's look a simple example of how we can implement the click event.
In this ng-click Example, we will have a counter variable which will increment in value when the user clicks a button.
<!DOCTYPE html>
<html>
<head>
<meta chrset="UTF 8">
<title>Event Registration</title>
</head>
<body ng-app="">
<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<h1> Guru99 Global Event</h1>
<button ng-click="count = count + 1" ng-init="count=0">
Increment
</button>
<div>The Current Count is {{count}}</div>
</body>
</html>
Code Explanation:
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
From the above output,
The ng-Show directive in AngularJS is used to show or hide a given specific HTML element based on the expression provided to the ng-show attribute. In the background, the HTML element is shown or hidden by removing or adding the .ng-hide CSS class onto the element. It is a predefined CSS class which sets the display to none.
<element ng-show="expression"> </element>
In the background, the element is shown or hidden by removing or adding the .ng-hide CSS class onto the element.
Let's look at an ng-show Example of how we can use the "ngshow event" directive to display a hidden element.
<!DOCTYPE html>
<html>
<head>
<meta chrset="UTF 8">
<title>Event Registration</title>
</head>
<body>
<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<h1> Guru99 Global Event</h1>
<div ng-app="DemoApp" ng-controller="DemoController">
<input type="button" value="Show Angular" ng-click="ShowHide()"/>
<br><br><div ng-show = "IsVisible">Angular</div>
</div>
<script type="text/javascript">
var app = angular.module('DemoApp',[]);
app.controller('DemoController',function($scope){
$scope.IsVisible = false;
$scope.ShowHide = function(){
$scope.IsVisible = true;
}
});
</script>
</body>
</html>
Code Explanation:
In the controller, we are attaching the "IsVisible" member variable to the scope object. This attribute will be passed to the ng-show angular attribute (step#2) to control the visibility of the div control. We are initially setting this to false so that when the page is first displayed the div tag will be hidden.
Note:- When the attributes ng-show is set to true, the subsequent control which in our case is the div tag will be shown to the user. When the ng-show attribute is set to false the control will be hidden from the user.
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:1
From the output,
The output now shows the div tag with the Angular text.
The ng-hide directive in AngularJS is a function using which an element will be hidden if the expression is TRUE. If the Expression is FALSE, the element will be shown. In the background, the element is shown or hidden by removing or adding the .ng-hide CSS class onto the element.
<element ng-hide="expression"> </element>
On the other hand with ng-hide, the element is hidden if the expression is true and it will be shown if it is false.
Let's look at the similar ng-hide Example as the one shown for ngShow to showcase how the ngHide attribute can be used.
<!DOCTYPE html>
<html>
<head>
<meta chrset="UTF 8">
<title>Event Registration</title>
</head>
<body>
<script src="https://code.angularjs.org/1.6.9/angular.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<h1> Guru99 Global Event</h1>
<div ng-app="DemoApp" ng-controller="DemoController">
<input type="button" value="Hide Angular" ng-click="ShowHide()"/>
<br><br><div ng-hide="IsVisible">Angular</div>
</div>
<script type="text/javascript">
var app = angular.module('DemoApp',[]);
app.controller('DemoController',function($scope){
$scope.IsVisible = false;
$scope.ShowHide = function(){
$scope.IsVisible = $scope.IsVisible = true;
}
});
</script>
</body>
</html>
Code Explanation:
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
From the output,
You can add AngularJS event listeners to your HTML elements by using one or more of these directives:
Tables are one of the common elements used in HTML when working with web pages. Tables in HTML are...
AngularJS is a JavaScript framework used for creating single web page applications. It allows you...
We have prepared the most frequently asked Angular interview questions and answers that acquaint...
What is Controller in AngularJs? A Controllers in AngularJs takes the data from the View,...
Displaying repeated information Sometimes we may be required to display a list of items in the...
What is Protractor Testing? PROTRACTOR is an automation and end-to-end behavior-driven testing tool...