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:
class Comp { constructor(existing: Injector) { const newInjector = ReflectiveInjector.resolveAndCreate(providers, existing) } }
To get more details read Difference between Reflective Injector and Injector in Angular.
How does Angular do this itself when it is loading new modules after bootstrapping, for example via the router?
It uses a bit different mechanism. When a new module instance is created it's passed the parent injector:
class NgModuleRef_ implements NgModuleData, InternalNgModuleRef<any> { constructor(..., public _parent: Injector) { initNgModule(this); }
And then when you request a token it uses this parent injector to resolve dependency if it's not found on the existing injector:
NgModuleRef_.get(token, notFoundValue = Injector.THROW_IF_NOT_FOUND) { return resolveNgModuleDep(...); } export function resolveNgModuleDep(...) { ... if (found) return instance; return data._parent.get(depDef.token, notFoundValue); <---------------- }