I have a number of elements that I want to be visible under certain conditions.
In AngularJS I would write
stuff
If you are using Bootstrap is as simple as this:
<div [class.hidden]="myBooleanValue"></div>
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
display: flex;
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
<div *ngIf="showGreeting">
Hello, there!
</div>
in bootstrap 4.0 the class "d-none" = "display: none!important;"
<div [ngClass]="{'d-none': exp}"> </div>
If you just want to use the symmetrical hidden
/shown
directives that AngularJS came with, I suggest writing an attribute directive to simplify the templates like so (tested with Angular 7):
import { Directive, Input, HostBinding } from '@angular/core';
@Directive({ selector: '[shown]' })
export class ShownDirective {
@Input() public shown: boolean;
@HostBinding('attr.hidden')
public get attrHidden(): string | null {
return this.shown ? null : 'hidden';
}
}
Many of the other solutions are correct. You should use *ngIf
where ever possible. Using the hidden
attribute can have unexpected styles applied, but unless you are writing components for others, you probably know if it is. So for this shown
directive to work, you will also want to make sure that you add:
[hidden]: {
display: none !important;
}
to your global styles somewhere.
With these you can use the directive like so:
<div [shown]="myVar">stuff</div>
with the symmetrical (and opposite) version like so:
<div [hidden]="myVar">stuff</div>
To add on to the shoulds - yous should also us a prefix like so [acmeShown]
vs just [shown]
.
The main reason I used a shown
attribute directive is for converting AngularJS code to Angular -AND- when the content that is being hidden contains container components that cause XHR round trips. The reason I don't just use [hidden]="!myVar"
is that often enough it is more complicated like: [hidden]="!(myVar || yourVar) && anotherVar" - yes I can invert that, but it is more error prone.
[shown]` is simply easier to think about.
this is what worked for me:
<div [style.visibility]="showThis ? 'visible' : 'hidden'">blah</div>
For anybody else stumbling across this issue, this is how I accomplished it.
import {Directive, ElementRef, Input, OnChanges, Renderer2} from "@angular/core";
@Directive({
selector: '[hide]'
})
export class HideDirective implements OnChanges {
@Input() hide: boolean;
constructor(private renderer: Renderer2, private elRef: ElementRef) {}
ngOnChanges() {
if (this.hide) {
this.renderer.setStyle(this.elRef.nativeElement, 'visibility', 'hidden');
} else {
this.renderer.setStyle(this.elRef.nativeElement, 'visibility', 'visible');
}
}
}
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.
<span hide="true"></span>
or
<span [hide]="anyBooleanExpression"></span>