Control dependency lifecycle with Scope
masterYou can control whether a dependency is cached or recreated every time using the third argument of provideFactory or provideClass, which accepts a Scope value.
Scope.Singleton(default): The dependency is cached within the specific injector and its children. Subsequent requests for the same dependency return the same instance.Scope.Transient: Caching is disabled. A fresh instance is created every time the dependency is requested.
// ... imports and setup
const fooProvider = injector
.provideFactory('log', loggerFactory, Scope.Transient)
.provideClass('foo', Foo, Scope.Singleton);
const foo = fooProvider.resolve('foo');
const fooCopy = fooProvider.resolve('foo');
const log = fooProvider.resolve('foo').log;
console.log(foo === fooCopy); // => true (Singleton)
// log will be a different instance if provided as Transient