Difference Between NgIf And Hidden Or Display:none In Angular
As explained in ngIf else article angular will convert the ngIf element to <ng-template> element. See the generated HTML DOM.
And the second div is not at all added the DOM. Angular will convert <ng-template> to a comment which gives us information about evaluated ngIf condition true or false.
hidden or display:none in Angular
Now we will see an example with hidden attribute in html5 and display none CSS.
<p [style.display]="'block'"> This paragraph is visible. </p> <p [style.display]="'none'"> This paragraph is hidden but still in the DOM. </p> <p hidden> This paragraph is hidden but still in the DOM. </p>And the generated HTML is
Irrespective of display hide or show the paragraph element will be added to the DOM.
Why ngIf directive remove the element rather than hide it?
Hiding and showing the element after rendering is fine if the paragraph or div element is very small and with simple interaction.
But with Angular, we can build rich applications some of the components may use too many resources. And even though the component is hidden, the component will be attached to its DOM element. It will keep on listening for events. Angular keep on checking for changes related to data bindings. The component behavior still exists even though it is hidden.
The component and its children components will be tie up resources. Memory burden might be high which results in poor performance, responsiveness can degrade and the user has no knowledge, why the application is slow.
So it’s better to add or remove elements to the component element to the HTML DOM rather than hiding or showing them.
But as I said earlier if the component is simple it is better to hide or show it, because component reinitialization operation could be expensive.
ngIf vs hidden or display none in Angular
ngIf | hidden or display none |
---|---|
Angular's structural directive | Normal HTML5 attribute |
No DOM element is added if ngIf evaluates to false. | DOM element will be added to HTML |
ngIf might be slow while adding removing elements to the DOM because of intializations | As the element is already added to DOM showing and hiding or very fast |
Ideal for rich angular applications | Ideal if the component is simple no much interaction |
I hope you understand all the differences between ngIf and hidden or display none in Angular
Từ khóa » Ng-show Vs Ng-hide Vs Ng-if
-
Define Ng-if, Ng-show And Ng-hide - GeeksforGeeks
-
Angularjs - When To Favor Ng-if Vs. Ng-show/ng-hide? - Stack Overflow
-
Sự Khác Biệt Giữa Ng-if Và Ng-show / Ng-hide? - HelpEx
-
What Is The Difference Between Ng-show / Ng-hide And Ng-if Directives?
-
AngularJS Tutorial => Ng-if Vs Ng-show
-
Angular Performance: Ng-show Vs Ng-if
-
What Is The Difference Between Ng-if And Ng-show/ng-hide
-
What Is The Difference Between Ng-if And Ng-show/ng-hide AngularJS?
-
What Is The Equivalent Of Ng-show And Ng-hide In Angular 2+? - Telerik
-
What Is The Difference Between Ng-if And Ng-show/ng-hide - Intellipaat
-
NgShow - AngularJS: API
-
NgHide - AngularJS API Docs
-
Angular Ng-if Directive - W3Schools
-
The Difference Between Ng-show Vs. Ng-if - DevelopIntelligence