AngularJS | ng-if Directive
The ng-if Directive in AngularJS is used to remove or recreate a portion of HTML element based on an expression. The ng-if is different from ng-hide because it completely removes the element in the DOM rather than just hiding the display of the element. If the expression inside it is false then the element is removed and if it is true then the element is added to the DOM.
Syntax:
<element ng-if="expression"> Contents... </element>
If expression returns true then the element is created and if the expression returns false then the element is completely removed.
Example 1: This example changes the content after clicking the button.
<!DOCTYPE html> <html> <head> <title>ng-hide Directive</title> <script src= </script> </head> <body ng-app="geek" style="text-align:center"> <h1 style="color:green"> GeeksforGeeks </h1> <h2>ng-if Directive</h2> <div ng-controller="app as vm"> <div ng-if="!vm.IsShow"> <input type="button" class="btn btn-primary" ng-click="vm.IsShow=!vm.IsShow" value="Sign in"> <p>Click to Sign in</p> </div> <div ng-if="vm.IsShow"> <button class="btn btn-primary" ng-click="vm.IsShow=!vm.IsShow"> Sign out </button> <p> GeeksforGeeks is the computer science portal for geeks. </p> </div> </div> <script> var app = angular.module("geek", []); app.controller('app', ['$scope', function ($scope) { var vm = this; }]); </script> </body> </html> |
Output:
Before click on button:

After click on button:

Example 2: This example added some content when check the checkbox.
<!DOCTYPE html> <html> <head> <title>ng-hide Directive</title> <script src= </script> <style> .geek { border: 1px solid black; padding: 10px; font-size: 15px; color: white; width: 50%; background: green; } </style> </head> <body ng-app style="padding:30px"> <h1 style="color:green"> GeeksforGeeks </h1> <h2>ng-if Directive</h2> <div> <input type="checkbox" ng-model="showDiv" /> <label for="showDiv"> Check it </label> <br><br> <div class="geek" ng-if="showDiv"> GeeksforGeeks is the computer science portal for geeks. </div> </div> </body> </html> |
Output:
Before marking checked to the checkbox:

After marking checked to the checkbox:

Recommended Posts:
- AngularJS | ng-cut Directive
- AngularJS | ng-app Directive
- AngularJS | ng-value Directive
- AngularJS | ng-jq Directive
- AngularJS | ng-src Directive
- AngularJS | ng-csp Directive
- AngularJS | Directive
- AngularJS | ng-srcset Directive
- AngularJS | ng-readonly Directive
- AngularJS | ng-selected Directive
- AngularJS | ng-paste Directive
- AngularJS | ng-required Directive
- AngularJS | ng-bind Directive
- AngularJS | ng-blur Directive
- AngularJS | ng-non-bindable Directive
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.



