DOM in AngularJS – 4 Major Directives for AngularJS HTML DOM

Free Angular course with real-time projects Start Now!!

In our last tutorial, we discussed Directives in AngularJS. Today, we will talk about DOM in AngularJS.Dom, which stands for the Document Object Model. In this Angular HTML DOM Tutorial, we are going to explore why DOM is used and different types of directives used in DOM: ng-disabled, ng-hide, ng-click, and ng-show with their syntax and examples.

So, are you ready to learn DOM in AngularJS?

DOM in AngularJS

Major Directives for AngularJS HTML DOM

1. What is DOM in AngularJS?

The logical structure of documents and documents are accessed and manipulated are defined using DOM elements. It defines events, methods, properties for all HTML elements as objects. DOM in AngularJS acts as an API (programming interface) for javascript.

Whenever a web page is loaded, the browser creates a Document Model Object (DOM) of that page.

Do you know how to use ng-model in AngularJS?

2. Why DOM is used?

A programmer can use DOM in AngularJS for the following purposes:

  • Documents are built using DOM elements.
  • A Programmer can navigate documents structure with DOM elements.
  • A programmer can add elements and content with DOM elements.
  • Programmer can modify elements and content with DOM elements.

3. Directives for AngularJS HTML DOM

We can use various directives to bind the application data to the attributes of DOM elements. Some of them are:

  • ng-disabled
  • ng-hide
  • ng-click
  • ng-show

i. ng-disabled

We can use ng-disabled for disabling a given control. Attributes of HTML elements can be disabled using ng-disabled directive.

We can use it by adding it to an HTML button and pass it to model

The Syntax od ng-disabled-

<input type = "checkbox" ng-model = "enableDisableButton">To disable a button
<button ng-disabled = "enableDisableButton">Click</button>

Example of ng-disabled-

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="" ng-init="value=true">
<p><button ng-disabled="value">Click Here!!</button></p>
<p><input type="checkbox" ng-model="value"/>Button</p>
<p>{{ value }}</p>
</div> 
</body>
</html>

Output-

Click Here!!

(checked) Button

true

Recommended reading – MVC Architecture & Working

The ng-disabled directive binds the application data value to the HTML button’s disabled attribute.

The ng-model directive binds the value of the HTML checkbox element to the value of value (In the above code value is the name of application data).

If the value of value evaluates to true, the button will disable and if the value of value evaluates to false, the button will not disable

Output-

Click Here!!

__ Button

false

ii. ng-show

We can use ng-show for showing a given control.

We can use it by adding it to an HTML button and pass it to model

The syntax of ng-show-

<input type = "checkbox" ng-model = "showHide1">To show a Button
<button ng-show = "showHide1">Click </button>

Let’s talk about AngularJS ng-include

Example of ng-show-

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
<input type = "checkbox" ng-model = "showhiddenbutton">Show button
<button ng-show = "showhiddenbutton">Click Here!</button>
</div>
</body>
</html>

Output-

__ Show button

When the checkbox is checked the output displays a button of click here! also.

Output-
(checked) Show button Click Here!

iii. ng-hide

We can use ng-hide for hiding a given control.

We can use it by adding it to an HTML button and pass it to model

The syntax of ng-hide-

<input type = "checkbox" ng-model = "showHide2">To hide a Button
<button ng-hide = "showHide2">Click</button>

Example of ng-hide-

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<div ng-app="">
<input type = "checkbox" ng-model = "hidebutton">Hide Button
<button ng-hide = "hidebutton">Click Here!</button>
</div>
</body>
</html>

Output-

__ Hide Button Click Here!

When you checked the checkbox, the button click here! will get hidden

Output-

(checked) Hide Button

Also, learn – Types of Modules in AngularJS

iv. ng-click

We can use ng-click for representing an angularjs click event.

We can use it by adding it to an HTML button and pass it to model

The syntax of ng-click-

<p>Number of click: {{ clickCounter }}</p>
<button ng-click = "clickCounter = clickCounter + 1">Click </button>

Example of ng-click-

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<div ng-app="">
<p>Total number of Clicks: {{ Counts }}</p>
<button ng-click = "Counts = Counts+ 1">Click Here!</button>
</div>
</body>
</html>

Output:

Total number of Clicks: 4

Click Here!

The number of times you will click on the button Click Here! will get displayed corresponding to the total number of clicks.

So, this was all about DOM in AngularJS. Hope you liked our explanation.

4. Conclusion

A document object model is used for manipulating and accessing the contents in documents created using HTML or XML. In the above article, we have discussed the directives that are used to bind the application data to the attributes of DOM elements such as ng-disabled, ng- show, ng-hide, ng-click. Furthermore, if you have any query, feel free to share with us, surely we will get back to you!

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *