ng-if

The ng-if Directive in AngularJS is used to remove or recreate a portion of an HTML element based on an expression. The ng-if is different from the 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.

Example: This example changes the content after clicking the button. 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>ng-if Directive</title>
    <script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
    </script>
</head>
 
<body ng-app="geek"
      style="text-align:center">
 
    <h1 style="color:green">
        w3wiki
    </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>
                w3wiki 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:

AngularJS Directives

Directives are markers in the Document Object Model(DOM). Directives can be used with any controller or HTML tag which will tell the compiler what exact operation or behavior is expected. There are some directives present that are predefined but if a developer wants he can create new directives (custom-directive).

Similar Reads

List of Directives:

The following table lists the important built-in AngularJS directives with their brief description:...

ng-app

The ng-app Directive in AngularJS is used to define the root element of an AngularJS application. This directive automatically initializes the AngularJS application on page load. It can be used to load various modules in AngularJS Applications....

ng-init

...

ng-model

The ng-init directive is used to initialize an AngularJS Application data. It defines the initial value for an AngularJS application and assigns values to the variables. The ng-init directive defines initial values and variables for an AngularJS application....

ng-controller

...

ng-bind

The ngModel is a directive that binds input, select, and textarea, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during validations in a form....

ng-repeat

...

ng-show

The ng-controller Directive in AngularJS is used to add the controller to the application. It can be used to add methods, functions, and variables that can be called on some event like click, etc to perform certain actions....

ng-readonly

...

ng-disabled

The ng-bind directive in AngularJS is used to bind/replace the text content of any particular HTML element with the value that is entered in the given expression. The value of specified HTML content updates whenever the value of the expression changes in the ng-bind directive....

ng-if:

...

ng-click:

Angular-JS ng-repeat directive is a handy tool to repeat a set of HTML code a number of times or once per item in a collection of items. ng-repeat is mostly used on arrays and objects....