7

I import a modal library of ng-bootstrap in a lazy module.

@NgModule({imports: [NgbModalModule]})

This library has a NgbModal service provided in root.

@Injectable({providedIn: 'root'})
class NgbModal {...}

I inject it into a component.

constructor(private modal: NgbModal) {}

I develop a class extension of that one.

export class CustomNgbModal extends NgbModal{...}

How can I override NgbModal type with CustomNgbModal?

Using modules will be

{provide: NgbModal, useClass: CustomNgbModal}

but using providedIn root metadata, no clue.

So, how can I override a module which is provided in root?

Serginho
  • 6,213
  • 2
  • 21
  • 45
  • do you want to make library think it is using CustomNgbModal and your code using it as CustomNgbModal? – Andrei Jan 29 '20 at 16:39
  • @Andrei I've injected NgbModal in a lot of componentes in my app. I want replace this type with my custom type. Like `{provide: NgbModal, useClass: CustomNgbModal}`but with modules provided in root. – Serginho Jan 29 '20 at 16:49

1 Answers1

-1

I beleieve what you are trying to achieve can be done with

//module
providers: [
  CustomNgbModal, // so you can inject your custom service
  {provide: NgbModal, useExisting: CustomNgbModal} // to the library would use this service instead of its own
]
Andrei
  • 5,715
  • 4
  • 11
  • Did you read the chunk "Using modules will be{provide: NgbModal, useClass: CustomNgbModal}"? I know how to override a provider which is defined in the module. The question is: How to override a provider which is defined with @Injectable({providedIn: root})? – Serginho Jan 29 '20 at 16:47