Each component can specify new Providers using its providers property in ComponentMetadata.
Is there a way to specify provider
There is one way to create component using ViewContainerRef, where is allowed to pass injector so I guess this should be possible but it is limited to create components dynamically:
Create Injector:
static create(options: {providers: StaticProvider[], parent?: Injector, name?: string}): Injector;
// @param injector The injector to use as the parent for the new component.
abstract createComponent(
componentFactory: ComponentFactory, index?: number, injector?: Injector,
projectableNodes?: any[][], ngModule?: NgModuleRef): ComponentRef;
Pseudo code:
class FooComponent {
constructor(
private readonly injector: Injector,
private readonly viewContainer: ViewContainerRef){
const customInjector = this.injector.create({ providers: [FooService], parent: injector });
this.viewContainer.createComponent(componentFactory, 0, customInjector );
...
}
}
Or similarly use Portal from Angular CDK.