Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 341
sooon Avatar asked Dec 31 '16 05:12

sooon


1 Answers

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} ]

like image 53
John Cotter Avatar answered Oct 18 '22 05:10

John Cotter