-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add parentInjector to Platform Factories #43198
Comments
Yes. Yes. Yes! This would help in several use cases. There is no way to share something between several bootstrapped apps that share the same platform. My approach currently is different, but also very unhandy! Allowing to provide own Injector or even additional Providers except My Approach: // An NgModule that provides everything I want to share at platform level
@NgModule({
providers: [
// Add here custom providers
]
})
export class CustomPlatfromProviderModule { }
/**
* Returns the CustomPlatfromProviderModule NgModuleRef
* Bootstrap, if necessary
*/
export const getPlatformModule = (function () {
// Closure holdung the singleton module ref
let platformModule: NgModuleRef<CustomPlatfromProviderModule>;
return () =>
new Promise<NgModuleRef<CustomPlatfromProviderModule>>(
resolve =>
platformModule
? resolve(platformModule)
: platformBrowser()
.bootstrapModule(CustomPlatfromProviderModule)
.then(moduleRef => {
platformModule ||= moduleRef;
resolve(platformModule);
})
);
})();
/**
* Bootstraps an NgModule with the custom Platform module
* @param ngModule
*/
export const customBootstrap = <T>(ngModule: Type<T>): Promise<NgModuleRef<T>> =>
getPlatformModule()
.then(pf =>
(getModuleFactory(ngModule.name) as NgModuleFactory<T>).create(pf.injector))
.catch((error: Error) => console.error(`Error trying to bootstrap mfe ${ngModule}\n${error?.message}`, error) as any) But This approach requires to provide @NgModule({
id: ExampleModule.name // <-- required for getModuleFactory to work
})
export class ExampleModule { } But finally, one would able to bootstrap with custom injector / providers
But all in all, very unhandy. cheers |
This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list. You can find more details about the feature request process in our documentation. |
The "platform" in Angular is intended to be the top level of the DI hierarchy. It's possible to extend the platform injector in several ways:
These options allow for a lot of extensibility of the platform concept, without needing an option for a parent injector. Are there use cases which can't be satisfied by one of the above approaches? |
It is a matter of tying Angular to code that exists outside of the Angular ecosystem. export function bridgeToAngular(lucidInjector: LucidInjector) {
const angularProviders = lucidInjector.providers.map((injectionKey) => {
return {
provide: injectionKey,
useFactory: () => {
return lucidInjector.get(injectionKey);
},
};
});
return [{provide: LucidInjector, useValue: lucidInjector}, angularProviders];
} This worked well for many years (2017-2021), but we ran into issues when we started adding the ability to add new We thought of trying to re-bridge everything in the lazily loaded NgModules, but then classes annotated with Which led us to the solution of providing our injector (well, a version of it that has an Angular-compatible API), as the parent of the platform's injector. Since tl;dr: We needed a way to augment the |
Which @angular/* package(s) are relevant/releated to the feature request?
platform-browser-dynamic, platform-browser, platform-server
Description
We would like to extend the functionality of the existing platform factories by adding a parent injector to the platform. The only way to do this currently involves copying providers from Angular source code, some of which are private exports, and some which have to be massaged in order to work at all.
This would allow us to change the code
to the much terser:
Proposed solution
Change the return type of
createPlatformFactory
to(extraProviders?: StaticProvider[], parentInjector?: Injector) => PlatformRef
The implementation would then simply pass
parentInjector
in when callingInjector.create
.Alternatives considered
Calling
createPlatform
directly is possible, but then it requires copying the list of built-in providers from the existing platforms. Such as copying_CORE_PLATFORM_PROVIDERS
. This is fragile as this would break anytime that list changes.The text was updated successfully, but these errors were encountered: