Assuming that <600px means mobile for you, you can use this observable and subscribe to it:
First we need the current window size. Thus, we create an observable that emits only one value: the current window size.
initial$ = Observable.of(window.innerWidth > 599 ? false : true);
Then we need to create another observable so that we know when the window was resized. For this we can use the "fromEvent" operator. To learn more about rxjs operators, visit: rxjs
resize$ = Observable.fromEvent(window, 'resize').map((event: any) => { return event.target.innerWidth > 599 ? false : true; });
Collapse these two threads to get our observable:
mobile$ = Observable.merge(this.resize$, this.initial$).distinctUntilChanged();
Now you can subscribe to it as follows:
mobile$.subscribe((event) => { console.log(event); });
Do not forget to unsubscribe :)
Flosut Mözil May 7 '17 at 17:34 2017-05-07 17:34
source share