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?
I find myself in the same situation with the difference than in my case the element was a flex container.If is not your case an easy work around could be
in my case due to the fact that a lot of browsers that we support still need the vendor prefix to avoid problems i went for another easy solution
where then the CSS is simple as
to leave then the displayed state handled by the default class.
Sorry, I have to disagree with binding to hidden which is considered to be unsafe when using Angular 2. This is because the hidden style could be overwritten easily for example using
The recommended approach is to use *ngIf which is safer. For more details, please refer to the official Angular blog. 5 Rookie Mistakes to Avoid with Angular 2
According to Angular 1 documentation of ngShow and ngHide, both of these directive adds the css style
display: none !important;
, to the element according to the condition of that directive (for ngShow adds the css on false value, and for ngHide adds the css for true value).We can achieve this behavior using Angular 2 directive ngClass:
Notice that for
show
behavior in Angular2 we need to add!
(not) before the ngShowVal, and forhide
behavior in Angular2 we don't need to add!
(not) before the ngHideVal.If your case is that the style is display none you can also use the ngStyle directive and modify the display directly, I did that for a bootstrap DropDown the UL on it is set to display none.
So I created a click event for "manually" toggling the UL to display
Then on the component I have showDropDown:bool attribute that I toggle every time, and based on int, set the displayDDL for the style as follows
If you are using Bootstrap is as simple as this:
myExpression may be set to true or false