Trajche Shoposki
Trajche Shoposki

Reputation: 221

How to dynamically set LocationStrategy class

How to dynamically configure AppModuleNgFactory in Angular 2 application compiled with ahead of time compiler?

LocationStrategy provider class should be set depending on window.isCordova environment variable

{
    provide: LocationStrategy,
    useClass: window.isCordova ? HashLocationStrategy : PathLocationStrategy
}

If the application is not compiled with AOT, it works without any issue. But when its compiled with AOT, LocationStrategy provider is always set to HashLocationStrategy.

Any idea how to accomplish this?

Upvotes: 1

Views: 421

Answers (1)

Avi
Avi

Reputation: 1964

You should do something like:

{ provide: LocationStrategy, useFactory: locationStrategyFactory, deps: [PlatformLocation] },

The locationStrategyFactory function should look like:

const locationStrategyFactory = (_platformLocation: PlatformLocation) => {
  return (isUseHash ?  new HashLocationStrategy(_platformLocation) : new PathLocationStrategy(_platformLocation) );
};

Upvotes: 1

Related Questions