Angular - Determine whether to use BrowserAnimationsModule or NoopAnimationsModule at runtime by importing both and utilizing Ahead of Time compilation

The issue at hand is:

I am trying to selectively import the NoopAnimationsModule only when I'm using Internet Explorer. In my app.module's imports array, I currently have:

imports: [AppConfig.IS_IE ? NoopAnimationsModule : BrowserAnimationsModule, .... ]

While this setup works during development, it encounters problems in production. I suspect that AOT compilation may be causing issues, placing the import before reaching the browser.

After some deliberation, I realized that importing both modules and configuring them at runtime might be a viable solution to determine which module's providers to use.

It's important to note that both of these modules offer similar services but differ in how they handle animations in Angular.

Is there a way to successfully implement this approach?

Answer №1

That is quite intriguing. Indeed, while AoT used to prevent this issue, it seems that with Ivy, the ability to execute it in your preferred manner should already exist. However, a dynamic value like that may not currently work as intended.

I just confirmed through testing that indeed, with a dynamic value, the functionality does not perform as expected at the moment.

If you are using Angular 9 or later, try implementing the following approach:

Assuming that your AppConfig.IS_IE is defined before importing the animation module, you could potentially achieve the desired outcome by creating a customized animations module. This solution leverages a specific property called ɵinj, which is not part of the public API and thus may change in future versions:

@NgModule({
  exports: [BrowserModule],
})
export class ConfigAnimationsModule {
  static forRoot(isIE: boolean) {
    return {
      ngModule: ConfigAnimationsModule,
      providers: isIE
        ? NoopAnimationsModule.ɵinj.providers
        : BrowserAnimationsModule.ɵinj.providers
    };
  }
}

Once implemented, you can import it as follows:

imports: [
  ConfigAnimationsModule.forRoot(AppConfig.IS_IE),
  //...
]

For Angular versions up to 8, you may utilize the private property ngInjectorDef. Please note that this method has not been tested extensively, so results may vary:

@NgModule({
  exports: [BrowserModule],
})
export class ConfigAnimationsModule {
  static forRoot(isIE: boolean) {
    return {
      ngModule: ConfigAnimationsModule,
      providers: isIE
        ? NoopAnimationsModule['ngInjectorDef'].providers
        : BrowserAnimationsModule['ngInjectorDef'].providers
    };
  }
}

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

Issue in Angular 6: TS2339 error - The property 'scrollUp' is not found on type 'JQueryStatic'

I have incorporated jQuery into my Angular 6 project using the following steps: npm install jquery npm install --save @types/jquery In addition, I have included jQuery in my angular.json file like this: "./node_modules/jquery/dist/jquery.min.js", "./nod ...

Establishing the correct data type to be returned from a fetch function in order to align with a specific custom type

My app has two interfaces defined: export interface Job { job_title: string, employer: string, responsibilities: string[] achievements: string[], start_date: string, end_date: string } export interface CreatedJob extends Job { ...

Unresolved peer dependencies in Angular 2

I recently set up an angular2 project using CLI, and my package.json file is structured as shown below: { "name": "thesis-proto", "version": "0.0.0", "license": "MIT", "angular-cli": {}, "scripts": { "ng": "ng", "start": "ng serve", ...

Delaying the tap after emitting an action in the Ngrx store

Within the realm of Ngrx store, I am currently utilizing 2 effects: @Effect({ dispatch: false }) toastSuccess$ = this.actions$ .pipe( ofType(TOAST_SUCCESS), map((action: any) => this.toastr.success(action.payload, &a ...

steps for signing in to garmin account with javascript

Attempting to set up an Oauth1 login for Garmin using Angular2 and standard http calls, but encountering a pre-flight OPTIONS call error on the initial request to oauth/request_token path. It seems like CORS is not enabled or something similar. Has anyone ...

Best practices for organizing an array of objects in JavaScript

I have an array of objects with nested arrays inside, and I need to restructure it according to my API requirements. [{ containerId: 'c12', containerNumber: '4321dkjkfdj', goods: [{ w ...

The inability to load tsc.ps1 is due to the system's script running restrictions

When I was running the tsc command on PowerShell, I encountered an error message that I hadn't seen before. Now I'm debating whether to adjust the security settings in PowerShell to fix this issue. I found a suggestion here: PowerShell says &quo ...

The ForbiddenError has struck again, this time wreaking havoc in the realms of Node.js, Express.js

I am currently adapting this GitHub sample application to utilize Express instead of KOA. However, I am encountering an Access Denied issue when the / route in Express attempts to load the index.html. What specific modifications are required in the code be ...

When the request's credentials mode is set to 'include', the 'Access-Control-Allow-Origin' header value in the response should not be the wildcard character '*'

I'm currently in the process of establishing a connection between my Angular application and Node.js using a GraphQL API, utilizing cookies/sessions for authentication purposes. The GraphQL Playground successfully interacts with the API. Here is how ...

`Understanding the outcome type by assessing the (potential) attributes with TypeScript`

Here is a detailed example of a problem with some code: type Mapper<T, R> = (data: T) => R; interface Config<T, R> { readonly mapper?: Mapper<T, R>; } function transform<T, R>(config: Config<T, R>, data: T) { return c ...

What is the best way to arrange a list in Angular2 using AngularFire2's FirebaseListObservable?

Looking to retrieve all users from a Firebase realtime database and organize them based on a score property. I managed to achieve this by using the variable users: FirebaseListObservable<any[]>; however, encountered the following errors: Type & ...

What is the best way to choose a slug page using the useRouter in Next

Is there a way to include all my posts in this array so I can use them for conditional styling in the header component? Currently, the header is white on most pages, but dark on the homepage, 404 page, and project pages. However, I am experiencing issues ...

"Error: The ionic-angular tabs module could not be located

After updating my angular libraries, I encountered a strange issue with my ionic angular project. When I run the app, I see the following error in the console: ERROR Error: Uncaught (in promise): Error: Cannot find module './tabs/tabs.module' Err ...

Easy steps for importing node modules in TypeScript

I'm currently navigating the world of TypeScript and attempting to incorporate a module that is imported from a node module. I have chosen not to utilize webpack or any other build tools in order to maintain simplicity and clarity. Here is the struct ...

The 'subscribe' property is not found in the type 'OperatorFunction<Response, Recipe[]>'

I'm encountering an issue while attempting to retrieve data from firebase "Property 'subscribe' does not exist on type 'OperatorFunction'" Any suggestions on what might be missing here? import { Injectable } from '@angula ...

Using Nginx and Tomcat to Serve a Spring and Angular Application

I have successfully deployed a Spring Boot and Angular app in a Tomcat Container on my server. The application functions perfectly on localhost. Currently, I am attempting to connect my domain with the application. However, when I access my domain, the A ...

The Next Js version "next" is at "14.2.3", indicating that the page is experiencing issues

After creating a project in Next.js version "14.2.3", I encountered this page with an error message. Surprisingly, I hadn't made any changes to my code when this occurred. https://i.sstatic.net/UcTnF3ED.png What could be the reason for this sudden i ...

Angular Component Provider based on conditions

My component relies on a service that can be provided by its parent component or create a new instance of the service based on a dynamic condition. However, specifying a provider causes the parent instance of the service to be lost. Is there a way to acces ...

Issues arise with transferring React component between different projects

My goal is to develop a React component that serves as a navigation bar. This particular component is intended to be imported from a separate file into my App.js. Currently, the component is designed to simply display a 'Hello world' paragraph, ...

The subcategory was not factored into my npm package

In my npm module 'ldap-pool', I'm facing an issue where the '@types/ldapjs' package, which is a dependency along with 'ldapjs', does not get installed when I add 'ldap-pool' to another project. This particular s ...