How to detect a mobile application and switch to another application using angular2

I am creating an application with a complex user interface. It’s actually so complicated that I don’t think I can use ionic to just create a mobile version of the same application. For example, media queries can change my css, but I will need a completely new structure and, therefore, a new application.

Ideally, I would like to have a standard Angular2 desktop app and Ionic mobile app. I would prefer that they be in the same project folder so that I can share some code with them. I'm not even sure that this is possible.

Is there a way so that I can determine if the mobile device is and displays a different root of the application / application?

+4
source share
1 answer

You might want to try DynamicComponentLoader. Disclaimer, I have not used this with Angular2 Alpha. However, my approach was to detect screen resolution and dynamically load the component into the main template. For smaller devices, I downloaded the tab layout and for a larger sensitive grid. You could learn something like ng2-responsive for more reliable detection of permissions and device types.

In Angular2 Alpha (sorry example)

Plunger example

template:

<div>
  <div #location></div>
</div>

component method:

setLayout(pSize:string) {
this.removeAll();

if(pSize === 'xs') {
  console.log('loading layout ' + pSize);

  this._dcl.loadNextToLocation(TabbedLayout, this._e).then((ref) => {
  ref.instance._ref = ref;
  this._children = ref;
});
} else {
  console.log('loading grid ' + pSize);

  this._dcl.loadNextToLocation(GridLayout, this._e).then((ref) => {
  ref.instance._ref = ref;
  this._children = ref;

});
}

}

: ( - ng2-responseive )

    import {Injectable} from '@angular/core';
    import {BehaviorSubject} from 'rxjs/BehaviorSubject';
    import {Observable} from 'rxjs/Observable';
    import 'rxjs/add/observable/fromEvent';
    import 'rxjs/add/operator/map';
    import 'rxjs/add/operator/pluck';
    import 'rxjs/add/operator/distinctUntilChanged';

    @Injectable()
    export class ResizeSvc {
       width$: Observable<number>;
       height$: Observable<number>;
       layout$: Observable<string>;
       constructor () {
    let windowSize$ = new BehaviorSubject(this.getWindowSize()); // most recent and subsequent values
    this.width$ = windowSize$.pluck('width').distinctUntilChanged(); 
    this.height$ = windowSize$.pluck('height').distinctUntilChanged();
    this.layout$ = windowSize$.pluck('layout').distinctUntilChanged(); // only observed distinct changes, e.g sm -> md -> lg, not lg -> lg -> lg 
    Observable.fromEvent(window, 'resize')
      .map(this.getWindowSize)
      .subscribe(windowSize$);
  }

  getWindowSize() {
    var size = 'na';
    if(window.innerWidth < 768) {
      size = 'xs';
    } else if (window.innerWidth < 992) {
      size = 'sm';
    } else if(window.innerWidth < 1200) {
      size = 'md'
    } else {
      size = 'lg';
    }
  return {
    height: window.innerHeight,
    width: window.innerWidth,
    layout: size
  };
}
}
+3

Source: https://habr.com/ru/post/1667087/


All Articles