The ScreenOrientation API provides the ability to set or lock the screen orientation (landscape or portrait), as well as detect when the orientation has changed.
// get current
console.log(ScreenOrientation.type); // logs the current orientation, example: 'landscape'
...
// set to landscape
ScreenOrientation.lock(ScreenOrientation.ORIENTATIONS.LANDSCAPE);
...
// allow user rotate
ScreenOrientation.unlock();
...
// detect orientation changes
ScreenOrientation.onChange().subscribe(
() => {
console.log("Orientation Changed");
});
// get current
console.log(window.ScreenOrientation.type); // logs the current orientation, example: 'landscape'
...
// set to landscape
window.ScreenOrientation.lock(ScreenOrientation.ORIENTATIONS.LANDSCAPE);
...
// allow user rotate
window.ScreenOrientation.unlock();
...
// detect orientation changes
window.ScreenOrientation.onChange().subscribe(
() => {
console.log("Orientation Changed");
});
// get current
console.log(window.ScreenOrientation.type); // logs the current orientation, example: 'landscape'
...
// set to landscape
window.ScreenOrientation.lock(ScreenOrientation.ORIENTATIONS.LANDSCAPE);
...
// allow user rotate
window.ScreenOrientation.unlock();
...
// detect orientation changes
window.ScreenOrientation.onChange().subscribe(
() => {
console.log("Orientation Changed");
});
// get current
console.log((<any>window).ScreenOrientation.type); // logs the current orientation, example: 'landscape'
...
// set to landscape
(<any>window).ScreenOrientation.lock((window).ScreenOrientation.ORIENTATIONS.LANDSCAPE);
...
// allow user rotate
(<any>window).ScreenOrientation.unlock();
...
// detect orientation changes
(<any>window).ScreenOrientation.onChange().subscribe(() => {
console.log("Orientation Changed");
});
// get current
console.log(window.ScreenOrientation.type); // logs the current orientation, example: 'landscape'
...
// set to landscape
window.ScreenOrientation.lock(ScreenOrientation.ORIENTATIONS.LANDSCAPE);
...
// allow user rotate
window.ScreenOrientation.unlock();
...
// detect orientation changes
window.ScreenOrientation.onChange().subscribe(
() => {
console.log("Orientation Changed");
});
onChange(): Observable <void>
Listen to orientation change event.
enum Orientation { LANDSCAPE : "landscape" // The orientation is either landscape-primary or landscape-secondary (sensor) LANDSCAPE_PRIMARY : "landscape-primary" // The orientation is in the primary landscape mode LANDSCAPE_SECONDARY : "landscape-secondary" // The orientation is in the secondary landscape mode PORTRAIT : "portrait" // The orientation is either portrait-primary or portrait-secondary (sensor) PORTRAIT_PRIMARY : "portrait-primary" // The orientation is in the primary portrait mode PORTRAIT_SECONDARY : "portrait-secondary" // The orientation is in the secondary portrait mode ANY : "any" // Any of the above }