sooon
sooon

Reputation: 4878

ionic2 - ScreenOrientation

I want to lock my screen orientation to landscape at particular page in my ionic2 app. So I import the plugin from ionic site and Cordova screen orientation plugin too:

import { ScreenOrientation } from 'ionic-native';

Then I tried call it in my constructor:

  constructor(public navCtrl: NavController,
              public orientation:ScreenOrientation
              ) {
                ScreenOrientation.lockOrientation('Landscape');
              }

But I got this error:

EXCEPTION: Error in ./Test class Test_Host - inline template:0:0 caused by: No provider for ScreenOrientation!

What seems to be the issue here?

Upvotes: 6

Views: 4472

Answers (2)

John Cotter
John Cotter

Reputation: 329

The error is stating there is no provider available for the ScreenOrientation "service". In order to use these providers they must first be declared in app.module.ts.

To add ScreenOrientation to your list of providers in app.module.ts:

  1. First add the import:
    import { ScreenOrientation } from '@ionic-native/screen-orientation';

  2. Then add ScreenOrientation to your list of providers in @NgModule:
    providers: [ StatusBar, SplashScreen, ScreenOrientation, {provide: ErrorHandler, useClass: IonicErrorHandler} ]

Upvotes: 10

Mete Cantimur
Mete Cantimur

Reputation: 1401

Delete the parameter “public orientation:ScreenOrientation” from the constructor function.

Upvotes: 0

Related Questions