Difference Between ng-if, ng-show and ng-hide in Angular. js, angular. jsng-if

Source: Internet
Author: User

Difference Between ng-if, ng-show and ng-hide in Angular. js, angular. jsng-if

Preface

We all know that when developing front-end pages using anularjs, ng-show, ng-hide, and ng-if functions are often used to control the display or hiding of page elements, what is the difference between them? Let's take a look at this article.

Implementation Principle

Ng-show/ng-hide controls the display and hiding of elements by modifying the CSS style. The corresponding DOM elements will always exist on the current page, ng-if dynamically adds and deletes page elements to the current page based on the expression value. If the value of the value assignment expression is false, the element is deleted from the page. Otherwise, an element is added. When creating an element, ng-if uses the Code Compiled by it. if the internal code of ng-if has been modified by other methods, the modification will only be effective for this display, after the page element is re-rendered, the modification effect will disappear, while ng-show/ng-hide can retain the status after the last modification of the dom element.

In terms of scope, there is also a difference between the two: When an element is deleted from the DOM by ng-if, the associated scope will also be destroyed. In addition, when it is added to the DOM again, a new scope will be generated, while ng-show and ng-hide will not.

The following code can be used with the browser Developer Tools to clearly identify the differences between the two:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Js

angular.module("app",[]).controller("MainCtrl",function($scope){});

The final HTML part of the page is as follows:

<div ng-controller="MainCtrl" class="ng-scope"> <div ng-show="false" class="ng-hide"> ng-show = false  </div> <div ng-show="true" class=""> ng-show=true  </div> <!-- ngIf: true --><div ng-if="true" class="ng-scope"> ng-if = true </div><!-- end ngIf: true --> <!-- ngIf: false --></div>

Ng-if/ng-hide (ng-show) has a big difference in principle, so the effect displayed in some cases is quite different.

Use ng-if and ng-show correctly

When using bootstrap, we often use button groups, that is, btn-group. If you observe them carefully, the first and last buttons in a button group have rounded corners, for example:

However, the buttons in the middle have no rounded corners, which looks more beautiful.

When used in combination with angular, you sometimes need to hide some buttons based on some conditions. When you hide the first or last button, some minor problems may occur.

Code:

 <div class="row" ng-controller='myCtrl'>  <div class="col-lg-offset-1">  <div class="btn-group">   <button class='btn btn-primary'>button1</button>   <button class='btn btn-primary'>button2</button>   <button class='btn btn-primary' ng-show='false'>button3</button>  </div>  </div> </div>

Effect

We found that the corner of button2 is missing, which is inconsistent with our expectation.

The problem is caused by ng-show, and we know that ng-show only changes the display attribute of the element.ng-show='false'Changestyle='display:none'The rounded corner disappears.

So what should we do to make it show rounded corners?

If we want to display the rounded corner, we should use ng-ifng-show='false' Changeng-if='false' You can.

The reason is: ng-if determines the creation and destruction of the current DOM element based on the value of the expression. If the expression returns true, it is created. Otherwise, it is destroyed. Destruction means that the DOM element is deleted from the page.

Effect

The rounded corner shows that button3 has been deleted from the page.

Further, we can use the developer tool to find that the DOM element does not exist:

Summary

The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.