What is the difference between ng-if and data-ng-if directives ?
The ng-if is a directive in AngularJS which is used to remove the HTML element if the value of the expression or variable is false, unlike ng-hide which just hides the HTML element from the DOM.
Syntax:
<element angular_directive=expression> Contents... </element>
There are few other options which behave like ng-if. There is no difference among them in functionality wise.
- ng:if
- ng_if
- x-ng-if
- data-ng-if
Note: The best practice is to use ng-if only.
The reason behind why these options come into the picture is that in AngularJS we refer to the directive using camel case (example:ngIf) but when we use it in HTML since HTML is case insensitive we use a dash-delimited form (example:ng-if) or other delimiters as mentioned in the list above. So the AngularJS normalizes (It means it converts the delimiter form into camelcase.) the element’s tag and figures out to which directive does the element belong.
Example 1: This example uses “data-ng-if” directive.
<!DOCTYPE html> <html> <head> <title> What is the difference between ng-if and data-ng-if directives ? </title> <script src= </script> </head> <body ng-app=""> <center> <h1 style="color:green"> GeeksforGeeks </h1> <input ng-model="var1"> <div data-ng-if="var1"> <h3> This will disappear if the value of input var1 is set to false and will appear again when true </h3> </div> </center> </body> </html> |
Output:

Example 2: This example uses “ng-if” directive.
<!DOCTYPE html> <html> <head> <title> What is the difference between ng-if and data-ng-if directives ? </title> <script src= </script> </head> <body ng-app=""> <center> <h1 style="color:green"> GeeksforGeeks </h1> <input ng-model="var1"> <div ng-if="var1"> <h3> This will disappear if the value of input var1 is set to false and will appear again when true </h3> </div> </center> </body> </html> |
Output:

Recommended Posts:
- Directives in JSP
- AngularJS | Directives
- Angular 7 | Directives
- Difference between JSP and ASP
- Web 1.0, Web 2.0 and Web 3.0 with their difference
- Difference between “!==” and “==!” in PHP
- Difference between ASP and ASP.NET
- What is the difference between GUI and CUI?
- What is the difference between CSS and SCSS ?
- Difference Between HTML and ASP
- Difference between ReactJS and Vue.js
- Difference between "." and "#" selector in CSS
- Difference between array() and [] in PHP
- Difference between Where and Group By
- What is the difference between the | and || or operator in php?
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.


