-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Ability to add services to modal injector #5275
Comments
Add providers property to ModalOptions so that services can be added to the injector for the modal. Closes valor-software#5275.
Add providers property to ModalOptions so that services can be added to the injector for the modal. Closes valor-software#5275.
Add providers property to ModalOptions so that services can be added to the injector for the modal. Closes #5275. Co-authored-by: Dmitriy Danilov <daniloff200@gmail.com> Co-authored-by: Dmitriy Shekhovtsov <valorkin@gmail.com>
this feature works only if providers has no other DI or providers are registered as part of Example: @Component()
export class RootModalComponent {
constructor(rootComponentModalService: RootComponentModalService) {}
}
@Component()
export class FeatureModalComponent {
constructor(featureComponentModalService: FeatureComponentModalService) {}
}
@Component()
export class FeatureWithDIModalComponent {
constructor(featureWithOtherDIService: FeatureWithOtherDIService) {}
}
@Injectable()
export class FeatureComponentModalService {}
@Injectable()
export class FeatureWithOtherDIService {
constructor(featureWithoutDIService: FeatureWithoutDIService) {}
}
@Injectable()
export class FeatureWithoutDIService {}
@NgModule({
imports: [FeatureModule],
declarations: [RootModalComponent],
providers: [RootComponentModalService]
})
export class RootModule {}
@NgModule({
declarations: [FeatureModalComponent, FeatureWithDIModalComponent],
providers: [FeatureComponentModalService, FeatureWithOtherDIService, FeatureWithoutDIService]
})
export class FeatureModule {}
platform.bootstrapModule(RootModule)
// usage
bsModalService.show(RootModalComponent) // works as expected
// dependecy is not provided for RootModule and cannot be found
bsModalService.show(FeatureModalComponent) // injection error
bsModalService.show(FeatureModalComponent, {providers: [FeatureComponentModalService]}) // works as expected
// first dependency is provided but cannot be injected since its dependency (FeatureWithoutDIService) is not found
bsModalService.show(FeatureModalComponent, {providers: [FeatureComponentModalService]}) // injection error
bsModalService.show(FeatureModalComponent, {providers: [FeatureComponentModalService, FeatureWithoutDIService]}) // works as expected I hope the examples are readable :) Hacks:
If it will be fixed (proposal): for now it works: let modalInjector({
providers: options.providers,
parent: rootInjector // this._injector in the code
}) to next view: let modalInjector({
providers: options.providers,
parent: options.parentInjector || rootInjector
}) |
My use case was:
It was the responsibility of the code calling |
@rseanhall Is it planned to add such an option to define a modal injector in the future? |
Currently,
BsModalService
does not give the ability to provide services through injection. It is hardcoded to provide theModalOptions
andBsModalRef
service in_showModal
. I have a service that needs to be injected deep into the component tree where it is impractical to pass it through usinginitialState
andInput
s.ModalOptions
should expose aproviders?: StaticProvider[]
property so that the caller can add more providers to the injector.The text was updated successfully, but these errors were encountered: