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?
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?
To hide and show div on button click in angular 6.
Html Code
Component .ts Code
this works for me and it is way to replace ng-hide and ng-show in angular6.
enjoy...
Thanks
in bootstrap 4.0 the class "d-none" = "display: none!important;"
For anybody else stumbling across this issue, this is how I accomplished it.
I used
'visibility'
because I wanted to preserve the space occupied by the element. If you did not wish to do so, you could just use'display'
and set it to'none'
;You can bind it to your html element, dynamically or not.
or
Just bind to the
hidden
propertySee also
issues
hidden
has some issues though because it can conflict with CSS for thedisplay
property.See how
some
in Plunker example doesn't get hidden because it has a styleset. (This might behave differently in other browsers - I tested with Chrome 50)
workaround
You can fix it by adding
To a global style in
index.html
.another pitfall
are the same as
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 thisfalse
is assigned asfalse
instead of"false"
.*ngIf
vs[hidden]
*ngIf
effectively removes its content from the DOM while[hidden]
modifies thedisplay
property and only instructs the browser to not show the content but the DOM still contains it.Use hidden like you bind any model with control and specify css for it:
HTML:
CSS:
OR
These are two ways to show/hide element. The only difference between
*ngIf
and[hidden]
is that*ngIf
will add or completely remove the element from DOM but with[hidden]
the browser will show/hide the element using CSS'sdisplay
property and it will remain in DOM.