Alfredo Calvimontes
Alfredo Calvimontes

Reputation: 11

Ionic 4 - Screen Orientation(portrait) does not work, NullInjectorError

I am new to Ionic 4 and I am trying to set the screen orientation to portrait, but I'm getting problems with the ScreenOrientation addon for Ionic 4. I have tried for a while other methods but those don't work either. And I can't make this addon work for this importation problem. I had too parameter confirmation in the constructor problems. I installed this apk on my cellphone and it doesn't work.

Here is the tabs.page.ts:

import { Component } from '@angular/core';
import { MenuController, Platform } from '@ionic/angular';
import { ScreenOrientation } from '@ionic-native/screen-orientation/ngx';

@Component({
  selector: 'app-tabs',
  templateUrl: './tabs.page.html',
  styleUrls: ['./tabs.page.scss'],
})
export class TabsPage {

  constructor( private platform: Platform,
               private screenOrientation: ScreenOrientation ) {
    this.platform.ready().then(() => {
    this.screenOrientation.unlock();
    this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.LANDSCAPE);
    });

   }

}

And here is app.module.ts:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { AlphabeticalOriginOrderPipe } from './pipes/alphabetical-origin-order.pipe';
import { NumericalCostOrderPipe } from './pipes/numerical-cost-order.pipe';
import { DateHourOrderPipe } from './pipes/date-hour-order.pipe';
import { ImageModalPageModule } from './image-modal/image-modal.module';
import { MenuComponent } from './components/menu/menu.component';
import { ScreenOrientation } from '@ionic-native/screen-orientation/ngx';

@NgModule({
  declarations: [AppComponent, AlphabeticalOriginOrderPipe, NumericalCostOrderPipe, DateHourOrderPipe, MenuComponent],
  entryComponents: [],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, ImageModalPageModule],
  providers: [
    StatusBar,
    ScreenOrientation,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

And here is the Error:

ERROR Error: Uncaught (in promise): NotSupportedError: screen.orientation.lock() is not available on this device.
    at resolvePromise (zone-evergreen.js:797)
    at zone-evergreen.js:707
    at ZoneDelegate.invoke (zone-evergreen.js:359)
    at Object.onInvoke (core.js:39699)
    at ZoneDelegate.invoke (zone-evergreen.js:358)
    at Zone.run (zone-evergreen.js:124)
    at zone-evergreen.js:855
    at ZoneDelegate.invokeTask (zone-evergreen.js:391)
    at Object.onInvokeTask (core.js:39680)
    at ZoneDelegate.invokeTask (zone-evergreen.js:390)

Upvotes: 1

Views: 1840

Answers (1)

Gydunhn_
Gydunhn_

Reputation: 19

If I remember correctly, you have to add these lines of code in the file config.xml, if you are using Ionic Cordova.

<preference name="Orientation" value="portrait" />
<preference name="Fullscreen" value="true" />

I think that should solve it.

And for that error, could you tell me what platform are you compiling on?

Upvotes: 2

Related Questions