I have:
showHide: false;
<div *ngIf="showHide">
Content
</div>
<button (click)="showHide = !showMap">
Button visited only max-width: 768px
</button>
In MAX-width: 768px, I have a button. On MIN-width: 768px button is hide. When I click on the button - it show a DIV. It work fine.
BUT
How to make *ngIf work only when MAX-width: 768px?
max-height: 768px
- button is display: block
- DIV is display: none (but when I click on the button = display:
block)
min-height: 768px:
- button is display: none
- DIV is display: block
At the moment when I resize from example 500px to 1000px: It depends on button I pressed
Use the below code into the OnInit()
by using above code we can put two css; based on desktop and mobile:-
or
CSS:
Thought I would add this to the question as I found in this particular situation
@angular/flex-layout
to be very helpful. https://github.com/angular/flex-layout.In particular you might be interested in the
fxHide
feature: https://github.com/angular/flex-layout/wiki/fxHide-API but this package makes working with a responsive application much easier in my opinion.You can show or hide content like so.
Doesnt show when 'small' or greater than 'medium' resolutions:
<div fxShow.sm="false" fxShow.gt-md="false" fxShow="true" ></div>
Only hides when at medium resolution
<div fxShow fxHide.md ></div>
The breakpoints can be custom but they have default ones already set.
This works when you resize the browser window
For future me or others if you need a solution that monitors the screen size and are using Observables/rxjs then this solution works:
Just came across this answer and I wanted something that worked responsively as well as on-resize. I set 992px as the breakpoint because I'm also using bootstrap
lg
breakpoints concurrently.Then in the HTML
<div *ngIf="isMobile === true>Some mobile element!</div>
The least lines solution:
In your TS file:
In HTML template:
Edit: Also, you can detect mobile using the following code:
This might be helpful to initialize
isMobileLayout
value before any resize happens:Or: