AngularJS ng-click, ng-show & ng-hide Directives
โก Smart Summary
AngularJS Events let a web application respond to user input such as mouse clicks, key presses, and hover actions. Directives including ng-click, ng-show, and ng-hide bind that behaviour declaratively in markup instead of through manual DOM listeners.

AngularJS Events
AngularJS Events are the functionalities that allow web applications to interact with user inputs such as mouse clicks, keyboard input, and mouse hover. Events must be handled in web applications in order to perform specific tasks and actions, and each one is triggered when the user performs the matching action.
When creating web applications, sooner or later your application needs to handle DOM events such as mouse clicks and moves, keyboard presses, and change events. AngularJS supplies directives that handle these events declaratively.
For example, if a button on the page should process something when clicked, the ng-click event directive provides that hook.
โ ๏ธ Support notice: AngularJS reached end of life on 31 December 2021 and receives no further patches or security fixes from the Angular team. The directives below remain accurate for maintaining existing AngularJS 1.x applications; for new work, use modern Angular, where these directives are replaced by event bindings such as (click) and by *ngIf.
What is ng-click Directive in AngularJS?
The โng-click directiveโ in AngularJS applies custom behaviour when an HTML element is clicked. It is normally used on buttons, since those are the most common place to attach click responses, and it is also used to raise alerts on click.
Syntax of ng-click in AngularJS
<element ng-click="expression"> </element>
Example of ng-click in AngularJS
In this ng-click example, a counter variable increments each time the user clicks a button.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Event Registration</title> </head> <body ng-app=""> <script src="https://code.angularjs.org/1.8.3/angular.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:
- The ng-init directive sets the local variable
countto 0. - The ng-click directive on the button increments
countby 1 on each click. - The interpolation
{{count}}displays the current value to the user.
Output:
The Increment button is displayed and the count is initially zero. Clicking it increments the value, as the image below shows.
What is ng-show Directive in AngularJS?
The ng-show directive shows or hides a given HTML element based on the expression supplied to the ng-show attribute. Internally the element is shown or hidden by removing or adding the .ng-hide CSS class, a predefined class that sets display to none.
Syntax of ng-show in AngularJS
<element ng-show="expression"> </element>
Example of ng-show in AngularJS
This example uses ng-show to reveal a hidden element.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Event Registration</title> </head> <body> <script src="https://code.angularjs.org/1.8.3/angular.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:
- The ng-click directive on the button references a
ShowHidefunction defined in the controllerDemoController. - The ng-show attribute is attached to the div containing the text Angular. This is the element whose visibility is controlled.
- In the controller, the
IsVisiblemember variable is placed on the scope object and initialised tofalse, so the div is hidden when the page first loads. With ng-show, a value of true displays the element and false hides it. - The
ShowHidefunction setsIsVisibleto true so the div becomes visible.
Output:
The div holding the text is not shown initially, because IsVisible starts as false. Clicking Show Angular sets it to true and the text becomes visible.
What is ng-hide Directive in AngularJS?
The ng-hide directive hides an element when its expression evaluates to TRUE, and shows the element when the expression is FALSE โ the exact inverse of ng-show. Internally it uses the same .ng-hide CSS class.
Syntax of ng-hide in AngularJS
<element ng-hide="expression"> </element>
Example of ng-hide in AngularJS
This example mirrors the ng-show example so the two directives can be compared directly.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Event Registration</title> </head> <body> <script src="https://code.angularjs.org/1.8.3/angular.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 = true; }; }); </script> </body> </html>
Code Explanation:
- The ng-click directive on the button references the
ShowHidefunction inDemoController. - The ng-hide attribute is attached to the div containing the text Angular.
IsVisibleis initialised tofalse. Because this is ng-hide rather than ng-show, a false value means the div is visible when the page first loads.- The
ShowHidefunction setsIsVisibleto true, which hides the div.
Output:
The div is shown initially because false is passed to ng-hide. Clicking Hide Angular passes true and the word disappears.
ng-show vs ng-hide vs ng-if: Key Differences
Three directives control whether the user sees an element, and choosing between them affects both behaviour and rendering performance. The first two only toggle a CSS class, so the element is always present in the page; the third adds and removes the element itself.
| Aspect | ng-show | ng-hide | ng-if |
|---|---|---|---|
| Element visible when | Expression is true | Expression is false | Expression is true |
| Element in the DOM | Always | Always | Only while true |
| Method used | Toggles .ng-hide class | Toggles .ng-hide class | Creates and destroys the element |
| Child scope | Shares the parent scope | Shares the parent scope | Creates a new child scope |
| Watchers when hidden | Still active | Still active | Removed |
| Best for | Frequent toggling of light content | Inverted conditions | Heavy content shown rarely |
Use ng-show or ng-hide when an element toggles often, because the element is never rebuilt and the toggle costs only a class change. Use ng-if for expensive sections shown rarely, since removing the element also removes its watchers and reduces digest cost on every cycle.
AngularJS Event Listener Directives
You can add AngularJS event listeners to HTML elements using one or more of these directives:
- ng-blur
- ng-change
- ng-click
- ng-copy
- ng-cut
- ng-dblclick
- ng-focus
- ng-keydown
- ng-keypress
- ng-keyup
- ng-mousedown
- ng-mouseenter
- ng-mouseleave
- ng-mousemove
- ng-mouseover
- ng-mouseup
- ng-paste
- ng-submit
Each directive accepts an expression, and the raw browser event is available inside it as $event when the handler needs details such as the pressed key or the click coordinates.









