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).