I have this Config file which has interface for data, default configuration of scrollbar, injection token to be able to inject this configuration and provider for modules that contains factory which returns deepClone of default config object:
export interface ScrollbarConfig {
name: string;
class: string;
options: MCustomScrollbar.CustomScrollbarOptions;
}
export const SCROLLBAR_CONFIG = new InjectionToken<ScrollbarConfig>('scrollbar.config');`
export const SCROLLBAR_CONFIG_DEFAULT: ScrollbarConfig = { ... }
export const SCROLLBAR_CONFIG_PROVIDER = {
provide: SCROLLBAR_CONFIG,
useFactory: () => {
return _.cloneDeep(SCROLLBAR_CONFIG_DEFAULT);
}
};
This is how add provider to my modules:
providers: [
SCROLLBAR_CONFIG_PROVIDER
]
And that's how I inject it in constructors of my components:
constructor(@Inject(SCROLLBAR_CONFIG) private scrollbarConfig: ScrollbarConfig) {}
So the idea is to get default config of scrollbar and then extend injected object in each component, so each component has own configuration. But for some reason injection gives me same instance even though I use provider with factory. I am pretty sure it makes a deepClone of default object, but then returns this same clonned object for every injection. I also tried to do it with creating class instead of injection token, but it behaved just the same.
I tried putting console.log() in factory function and it printed only once so obviously this is the problem, but how can I force it to really provide different instances?
providers: [SCROLLBAR_CONFIG_PROVIDER]for each component where you want to get an instance, not in module. Hierarchical injector works like that.