Transformation of Ionic 2 ScreenOrientation Plugin

Can someone assist me with this issue? A while back, my Ionic 2 app was functioning correctly using the ScreenOrientation Cordova plugin and the following code:

window.addEventListener('orientationchange', ()=>{
  console.info('DEVICE ORIENTATION CHANGED!');
  console.debug(ScreenOrientation.orientation);
  if(ScreenOrientation.orientation.type.indexOf('landscape') !== -1){
    this.screenOrientationIsPortrait = false;
  } else if(ScreenOrientation.orientation.type.indexOf('portrait') !== -1){
    this.screenOrientationIsPortrait = true;
  }
});

Upon installing the latest versions of Ionic 2 and the cordova ScreenOrientation plugin on a new laptop and using the same code, I encountered a compile time error:

Property 'type' does not exist on type 'string'.

I attempted to use the examples from the ScreenOrientation plugin's GitHub repository:

window.addEventListener('orientationchange', ()=>{
  console.info('DEVICE ORIENTATION CHANGED!');
  console.debug(ScreenOrientation.orientation);
  if(screen.orientation.type.indexOf('landscape') !== -1){
    this.screenOrientationIsPortrait = false;
  } else if(screen.orientation.type.indexOf('portrait') !== -1){
    this.screenOrientationIsPortrait = true;
  }
});

However, this code also resulted in a compile time error:

Property 'orientation' does not exist on type 'Screen'

It seems like a TypeScript error rather than a version conflict. How can I diagnose and resolve these errors? Any suggestions or guidance would be greatly appreciated. Thank you!

Github repo examples: https://github.com/apache/cordova-plugin-screen-orientation

Ionic 2 Examples: https://ionicframework.com/docs/v2/native/screen-orientation/

Answer №1

You have the ability to determine screen orientation changes even without the need for installing a plugin. Simply access the window property orientation.

window.addEventListener('orientationchange', () => {
      console.info('Screen orientation has changed!');
      console.debug(ScreenOrientation.orientation);
      switch (window.orientation) {
        case -90:
        case 90:
              console.log("Landscape orientation");
              this.screenOrientationIsPortrait = true;
              break;
        case 0:
             console.log("Portrait orientation");
              this.screenOrientationIsPortrait = false;
             break;
      }      
    });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using routerLink for linking to multiple components

Below is the anchor tag provided: <a class="uppercase" routerLink="settings/pressure" routerLinkActive="text-success" (click)="closeModal()" > <div class="pad-btm"> PRESSURE </div> </a> I need to include another route ...

Error: Unable to cast value to an array due to validation failure

I'm currently working on integrating Typegoose with GrqphQL, MongoDB, and Nest.js for a project. My goal is to create a mutation that will allow users to create a post. I have set up the model, service, and resolver for a simple Post. However, when I ...

The variable "theme" is referenced prior to being initialized

https://i.stack.imgur.com/QL0pa.png One of the variables in my code, theme, is set to be assigned a value from a for loop: let theme: Theme for (const themeObj of themeList) { const [muiThemeName, muiTheme] = Object.entries(themeObj)[0]!; if (muiThem ...

I want to modify a class in Angular 8 by selecting an element using its ID and adjust the styling of a div

Is it possible to dynamically add and remove classes using getElementById in Angular 8? I need to switch from 'col-md-12' to 'col-md-6' when a user clicks on the details icon. I also want to modify the style of another div by setting d ...

What is the process for importing a TypeScript module from the local directory?

I am currently working on a TypeScript module with plans to eventually release it on NPM. However, before publishing, I want to import the module into another project hosted locally for testing purposes. Both projects are written in TypeScript. The TypeSc ...

Angular (2/4) application utilizing custom-named routing within a single page architecture

I'm currently working on an Angular application that consists of a login component and a home component which serves as the main handler for the entire single page application. Additionally, I have three more components named users, each with function ...

Issue resolved: Mysterious fix found for background images not displaying in NextJS React components

I am having trouble displaying a background image on an element in NextJs using Typescript and Tailwind. I do not believe it is a TypeScript issue since I am not receiving any errors or IntelliSense warnings. Below is the code I am working with: var classn ...

Styling <Link> component with styled-components: A step-by-step guide

Utilizing the Link component from @material-ui/core/Link in my TypeScript code was initially successful: <Link href="#" variant="body2"> Forgot? </Link> However, I am exploring the transition to styled-components located in a separate file. ...

Can the contents of a JSON file be uploaded using a file upload feature in Angular 6 and read without the need to communicate with an API?

Looking to upload a JSON file via file upload in Angular (using version 6) and read its contents directly within the app, without sending it to an API first. Have been searching for ways to achieve this without success, as most results are geared towards ...

Is it feasible to mock a defined function in Typescript for a unit test scenario?

Currently, I am working on typescript code that compiles into javascript, gets bundled with rollup, and is utilized by a framework. This framework exposes a library to me in the global scope, taking the form of a function: fun({ prop1: number, ...

What might be the reason why the custom markers on the HERE map are not displaying in Angular?

I'm having trouble displaying custom icons on HERE maps. Despite not receiving any errors, the icons are not showing up as expected. I have created a demo at the following link for reference: https://stackblitz.com/edit/angular-ivy-zp8fy5?file=src%2Fa ...

Struggling with creating a generic TypeScript structure?

My goal is to manipulate data structured like this: const exampleState = { elements : { element1: { values: { value1: 10, value2: 10, }, elementDetails : { detail1 : { values: { value1: ...

Error message: "Using AngularFire 2 caused an issue as Firebase is not identified as a

I am interested in creating a sample app using Ionic 2 and AngularFire 2. After successfully integrating AngularFire 2 into Ionic 2, I encountered an error while trying to retrieve data from Firebase. The error message displayed in the browser console: ` ...

Strict mode error occurs when attempting to assign a value to ngComponentOutlet that is incompatible with the type of the lazy-loaded component

I am attempting to implement lazy loading for a component in Angular 11 (strict mode) using guidance from this tutorial. Dealing with strict mode has been challenging as there are very few resources available that cater to it. The goal is to have a compon ...

Here is a way to trigger a function when a select option is changed, passing the selected option as a parameter to the function

Is there a way to call a function with specific parameters when the value of a select element changes in Angular? <div class="col-sm-6 col-md-4"> <label class="mobileNumberLabel " for="mobilrNumber">Select Service</label> <div cla ...

Determining the typing of a function based on a specific type condition

I have created a unique type structure as shown below: type Criteria = 'Criterion A' | 'Criterion B'; type NoCriteria = 'NO CRITERIA'; type Props = { label?: string; required?: boolean; disabled?: boolean; } & ( | ...

The eslint rule 'import/extensions' was not found in the definition

I'm encountering two errors across all TypeScript files in ESLint on VS Code: Not able to find definition for rule 'import/extensions'.eslint(import/extensions) Not able to find definition for rule 'import/no-extraneous-dependencies&apo ...

The functionality to generate personalized worldwide timezone pipe is not functioning

I'm completely new to Angular and I've been working on creating a custom pipe for adjusting timezones. The idea is to allow users to select their preferred timezone and have the offset applied accordingly. To start, I created a file called timez ...

Using Angular's dependency injection in a project that has been transpiled with Babel

I am currently attempting to transpile my Angular 6 project, which is written in TypeScript, using the new Babel 7. However, I am facing challenges with getting dependency injection to function properly. Every time I try to launch the project in Chrome, I ...

Removing scrollbar from table in React using Material UI

I successfully created a basic table using react and material UI by following the instructions found at: https://material-ui.com/components/tables/#table. The table is functioning properly, but I am finding the scrollbar to be a bit inconvenient. https:// ...