Good Place. Good Things. Good Time.

AngularJs onclick

AngularJs onclick

Introduction to AngularJs onclick

AngularJS ng-click is an In-Built AngularJS directive that is mainly used to handle the click events on the HTML view and processing the data in the controller as per requirements. The ng-click directive can be used with multiple HTML elements such as button, input, select, checkbox, etc. This provides the developer ability to define custom behavior whenever an element is clicked. In this topic, we are going to learn about AngularJs onclick.


There are various HTML elements with which the ng-click directive is used

Using ng-click with input types

  1. Div/span

  1. Img

AngularJs onclick

  1. Button

  1. input

Using ng-click with textarea

How does the ng-click directive work in AngularJS?

In the AngularJS framework, it is very important to know that all the In-Built directive which the AngularJS framework has provisioned will always be denoted with the ng prefix.

The ng-click directive functions are based on the event click, which means as soon as HTML view experiences a click (a mouse click) on a particular HTML element, then its corresponding ng-click expression is evaluated. The expression inside an ng-click directive can be a function call where function declaration is in the controller; expression can also be directly written in the HTML view and evaluated there itself. It is a good practice to write the complex logic inside a function defined in the controller and then invoke the HTML view function. The ng-click directive will keep a watch on the onclick event of the browser and gets triggered whenever the click is triggered. The ng-click directive is executed at priority level 0, and the Event object is available as $event.


Below mentioned are an example of AngularJs onclick:


Product Cart


Total Products selected is = {{count}}

Using image

AngularJs onclick

The above example shows different ways of using the ng-click directive in HTML view in an AngularJS application. We have taken various HTML elements to showcase the use of the ng-click directive in the HTML template. This directive is used with different input types such as button, checkbox, text, tel, email, etc., and is also used for displaying selected values from a dropdown and textarea.

This is an example of a list of products is iterated in HTML view using ng-repeat using the product in products.

This is an example of using the ng-click directive with a button HTML element; whenever the button is clicked in HTML view, then addProduct function will get executed

AngularJs onclick

This is an example of using ng-click directive in img tag whenever the image is clicked then display details  function will get executed which is defined, controller


Output: 1 – Add and Remove Buttons in Product Cart

AngularJs onclick output 1

Output 2: total products count after adding 4 products and removing 1

AngularJs onclick output 2

Output 3: Using Image tag

output 3

Output 4: After clicking on the image

output 4

Conclusion – AngularJs onclick

ng-click is a directive in AngularJS that comes with a lot of importance in AngularJS application. This directive is used for handling the click event. Knowing where and when ng-click will get triggered is very important. There are various HTML tags with which this directive can be used.

Recommended Articles

This is a guide to AngularJs onclick. Here we discuss how the ng-click directive works in AngularJS and various HTML elements with which the ng-click directive is used. You may also look at the following articles to learn more –

  1. AngularJS ng-class
  2. AngularJS ng-disabled
  3. Angular 2 Directives
  4. AngularJS Filters

The post AngularJs onclick appeared first on EDUCBA.

Read original article here: AngularJs onclick

Read original article here: AngularJs onclick

Disclaimers and Denial of responsibility..!

Denial of responsibility! is an automatic aggregator of all media around the world. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – . The content will be deleted within 72 hours.
You might also like
Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.