SoFunction
Updated on 2025-04-12

AngularJS basic ng-if directive usage

AngularJS ng-if directive

AngularJS instance

Uncheck and remove content:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="/libs//1.4.6/"></script>
</head>
<body ng-app="">

reserve HTML: <input type="checkbox" ng-model="myVar" ng-init="myVar = true">

<div ng-if="myVar">
<h1>Welcome</h1>
<p>Welcome to my home.</p>
<hr>
</div>

<p>When the check box is unchecked DIV Elements will be removed。</p>
<p>When check box is re-selected,DIV The element will be redisplayed。</p>

</body>
</html>

Definition and usage

ng-if Directives are used to remove HTML elements when the expression is false.

If the result of the execution of the if statement is true, the remove element will be added and displayed.

ng-ifDirectives are different from ng-hide, where ng-hide hides elements, while ng-if removes elements from the DOM.

grammar

<element ng-if="expression"></element>

All HTML elements support this directive.

Parameter value

 

value describe
expression If the expression returns false, the entire element will be removed, and if true, the element will be added.

The above is a compilation of the basic knowledge of AngularJS. We will continue to add it later. Thank you for your support.