AngularJS | ng-click Directive

The ng-click Directive in AngluarJS is used to apply custom behavior when an element is clicked. It can be used to show/hide some element or it can popup alert when button is clicked.

Syntax:

<element ng-click="expression"> Contents... </element>

Example 1: This example uses ng-click Directive to display an alert message after clicking the element.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>ng-click Directive</title>
  
    <script src=
    </script>
</head>
   
<body ng-app="geek" style="text-align:center">
      
    <h1 style="color:green">GeeksforGeeks</h1>
    <h2>ng-click Directive</h2>
      
    <div ng-controller="app">
        <button>
            <a href="" ng-click="alert()">
                Click Here
            </a>
        </button>
    </div>
      
    <script>
        var app = angular.module("geek", []);
        app.controller('app', ['$scope', function ($app) {
            $app.alert = function () {
                alert("This is an example of ng-click");
            }
        }]);
    </script>
</body>
  
</html>

chevron_right


Output:
Before clicking the button:
ngclick
After clicking the button:
ngclick

Example 2: This example uses ng-click Directive to display some content after clicking the element.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>ng-click Directive</title>
  
    <script src=
    </script>
</head>
   
<body ng-app="" style="text-align:center">
      
    <h1 style="color:green">GeeksforGeeks</h1>
    <h2>ng-click Directive</h2>
      
    <form name="form">
        <div ng-hide="isShow">
            Enter Name:  <input type="text" required ng-model="Name" />
              
            <br><br>
              
            <input type="button" ng-disabled="form.$invalid"
                    ng-click="isShow = true" value="Sign in" />
        </div>
          
        <div ng-show="isShow">
            Sign in successful.<br>
            <input type="button" ng-click="isShow = false;Name=''"
                    value="Logout" />
        </div>
    </form>
</body>
  
</html>

chevron_right


Output:
Before clicking the button:
ngclick
After clicking the button:
ngclick



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.