A framework-agnostic way of phrasing this question is "How to register another service with the service locator?"
The Injector is set up to be immutable, both the interface and the implementation.
interface Injector {
abstract get(token: any, notFoundValue?: any): any;
}
interface https://github.com/angular/angular/blob/master/packages/core/src/di/injector.ts implementation https://github.com/angular/angular/blob/master/packages/core/src/di/reflective_injector.ts
How do you add another provider (dynamically, not via a module)?
How does Angular do this itself when it is loading new modules after bootstrapping, for example via the router?
In order to add a provider to the existing injector you have to extend it by creating a new injector and pass the parent injector:
To get more details read Difference between Reflective Injector and Injector in Angular.
It uses a bit different mechanism. When a new module instance is created it's passed the parent injector:
And then when you request a token it uses this parent injector to resolve dependency if it's not found on the existing injector: