I have a number of elements that I want to be visible under certain conditions.
In AngularJS I would write
stuff
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
<div class="dropdown">
<button class="btn btn-default" (click)="manualtoggle()" id="dropdownMenu1" >
Seleccione una Ubicación
<span class="caret"></span>
</button>
<ul class="dropdown-menu" [ngStyle]="{display:displayddl}">
<li *ngFor="let object of Array" (click)="selectLocation(location)">{{object.Value}}</li>
</ul>
</div>
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
showDropDown:boolean;
displayddl:string;
manualtoggle(){
this.showDropDown = !this.showDropDown;
this.displayddl = this.showDropDown ? "inline" : "none";
}
<div [hidden]="myExpression">
myExpression may be set to true or false
Best way to deal with this issue using ngIf
Because this well prevent getting that element render in front-end,
If you use [hidden]="true"
or style hide [style.display]
it will only hide element in front end and someone can change the value and visible it easily,
In my opinion best way to hide element is ngIf
<div *ngIf="myVar">stuff</div>
and also If you have multiple element (need to implement else also) you can Use <ng-template>
option
<ng-container *ngIf="myVar; then loadAdmin else loadMenu"></ng-container>
<ng-template #loadMenu>
<div>loadMenu</div>
</ng-template>
<ng-template #loadAdmin>
<div>loadAdmin</div>
</ng-template>
sample ng-template code
To hide and show div on button click in angular 6.
Html Code
<button (click)=" isShow=!isShow">FormatCell</button>
<div class="ruleOptionsPanel" *ngIf=" isShow">
<table>
<tr>
<td>Name</td>
<td>Ram</td>
</tr>
</table>
</div>
Component .ts Code
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent{
isShow=false;
}
this works for me and it is way to replace ng-hide and ng-show in angular6.
enjoy...
Thanks
for me, [hidden]=!var
has never worked.
So, <div *ngIf="expression" style="display:none;">
And, <div *ngIf="expression">
Always give correct results.
There are two examples on Angular documents https://angular.io/guide/structural-directives#why-remove-rather-than-hide
A directive could hide the unwanted paragraph instead by setting its display style to none.
<p [style.display]="'block'">
Expression sets display to "block".
This paragraph is visible.
</p>
<p [style.display]="'none'">
Expression sets display to "none".
This paragraph is hidden but still in the DOM.
</p>
You can use [style.display]="'block'" to replace ngShow and [style.display]="'none'" to replace ngHide.