Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of ngShow and ngHide in Angular 2+?

I have a number of elements that I want to be visible under certain conditions.

In AngularJS I would write

<div ng-show="myVar">stuff</div> 

How can I do this in Angular 2+?

like image 736
Mihai Răducanu Avatar asked Feb 23 '16 12:02

Mihai Răducanu


People also ask

What is the equivalent of ngShow and ngHide in angular?

ngShow and ngHide are two ng directives in AngularJS used to show and hide the elements respectively.

What is ngHide in angular?

Definition and Usage The ng-hide directive hides the HTML element if the expression evaluates to true. ng-hide is also a predefined CSS class in AngularJS, and sets the element's display to none .

What is difference between ngIf and ngShow?

Basic Difference between ng-if, ng-show and ng-hideng-if can only render data whenever the condition is true. It doesn't have any rendered data until the condition is true. ng-show can show and hide the rendered data, that is, it always kept the rendered data and show or hide on the basis of that directives.

What is the difference between ngIf and hidden?

The main difference between angular ngIf directive & hidden or display:none is ngIf will add or remove the element from DOM based on condition or expression. hidden attribute in html5 and display none CSS will show or hide the HTML element.


2 Answers

The hidden property can be used for that

[hidden]="!myVar" 

See also

  • https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/hidden

issues

hidden has some issues though because it can conflict with CSS for the display property.

See how some in Plunker example doesn't get hidden because it has a style

:host {display: block;} 

set. (This might behave differently in other browsers - I tested with Chrome 50)

workaround

You can fix it by adding

[hidden] { display: none !important;} 

To a global style in index.html.

another pitfall

hidden="false" hidden="{{false}}" hidden="{{isHidden}}" // isHidden = false; 

are the same as

hidden="true" 

and will not show the element.

hidden="false" will assign the string "false" which is considered truthy.
Only the value false or removing the attribute will actually make the element visible.

Using {{}} also converts the expression to a string and won't work as expected.

Only binding with [] will work as expected because this false is assigned as false instead of "false".

*ngIf vs [hidden]

*ngIf effectively removes its content from the DOM while [hidden] modifies the display property and only instructs the browser to not show the content but the DOM still contains it.

like image 176
Günter Zöchbauer Avatar answered Oct 23 '22 01:10

Günter Zöchbauer


Use the [hidden] attribute:

[hidden]="!myVar" 

Or you can use *ngIf

*ngIf="myVar" 

These are two ways to show/hide an element. The only difference is: *ngIf will remove the element from DOM while [hidden] will tell the browser to show/hide an element using CSS display property by keeping the element in DOM.

like image 43
Ali Shahzad Avatar answered Oct 23 '22 03:10

Ali Shahzad