Any field can be updated with ng-click using a custom JavaScript function. For this, we can make a clickable object in an HTML (usually a button) and attach a ng-click directive with it that calls this custom function. 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 a button is clicked.
Syntax:
<element ng-click="expression"> Contents... </element>
Example 1: This example calls a function to change the field value after click on the button.
<!DOCTYPE html> <html ng-app="example"> <head> <title> How to directly update a field by using ng-click in AngularJS? </title> <script src= </script> </head> <body> <div ng-controller="basicCntrl"> <h1 style="color:green"> Hello {{name}}! </h1> <!-- on button click the change() function is called from $scope --> <button type="button" ng-click="change()"> Click! </button> </div> <script type="text/javascript"> var app = angular.module('example', []); app.controller('basicCntrl', function($scope) { $scope.name = "GFG"; $scope.change = function() { this.name = 'GeeksForGeeks'; } }); </script> </body> </html> |
Output:
-
Before Click on the Button:

- After Click on the Button:

The button calls the change function which changes the variable name. The change reflected on the page because of the {{name}} syntax. For simpler logic such as the one shown in example 1, we can avoid calling the function and changing the variable inside the ng-click directive.
Example 2: This example changes the variable name inside the ng-click directive.
<!DOCTYPE html> <html ng-app="example"> <head> <title> How to directly update a field by using ng-click in AngularJS? </title> <script src= </script> </head> <body> <div ng-controller="basicCntrl"> <h1 style="color:green"> Hello {{name}}! </h1> <!-- on button click the name is changed directly --> <button type="button" ng-click="name='GeeksForGeeks'"> Click! </button> </div> <script type="text/javascript"> var app = angular.module('example', []); app.controller('basicCntrl', function($scope) { $scope.name = "GFG"; }); </script> </body> </html> |
Output:
-
Before Click on the Button:

- After Click on the Button:

Also, We can use other HTML tags to make the ng-click work such as a paragraph tag.
Example 3: This example uses a paragraph tag to change the heading content.
<!DOCTYPE html> <html ng-app="example"> <head> <title> How to directly update a field by using ng-click in AngularJS? </title> <script src= </script> </head> <body> <div ng-controller="basicCntrl"> <h1 style="color:green"> Hello {{name}}! </h1> <!-- on paragraph click the name is changed directly --> <p ng-click="name='GeeksForGeeks'"> Click! </p> </div> <script type="text/javascript"> var app = angular.module('example', []); app.controller('basicCntrl', function($scope) { $scope.name = "GFG"; }); </script> </body> </html> |
Output:
-
Before Click on the paramgraph element ‘Click’:

-
After Click on the paramgraph element ‘Click’:

Recommended Posts:
- How to get all CSS styles that are applied directly to an element using JavaScript ?
- How to send data of HTML form directly to JSON file?
- How to get the value of text input field using JavaScript?
- How to disable scroll to change number in <input type="number"> field using JavaScript/jQuery?
- How to force Input field to enter numbers only using JavaScript ?
- How to Adjust the Width of Input Field Automatically using JavaScript ?
- How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
- How to validate if input in input field exactly equals to some other value using express-validator ?
- How to validate if input in input field has alphanumeric characters only using express-validator ?
- How to validate if input in input field has base64 encoded string using express-validator ?
- How to validate if input in input field has boolean value using express-validator ?
- How to validate if input in input field has ASCII characters using express-validator ?
- How to validate if input in input field has base 32 encoded string using express-validator ?
- How to validate if input in input field has alphabets only using express-validator ?
- How to validate if input in input field has BIC or Swift code only using express-validator ?
- How to validate if input in input field has integer number only using express-validator ?
- How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
- How to validate if input in input field has decimal number only using express-validator ?
- How to validate if input in input field is a valid date using express-validator ?
- How to validate if input in input field has lowercase letters only using express-validator ?
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.

