Every angular provider is a singleton

Georgi Parlakov
ng-gotchas
Published in
1 min readApr 6, 2018

#ng-gotchas #3

Earth and the other planets are not aligned with the plane of milky way galaxy…

Are they really? provide: useClass, useValue, useExistingsure, but useFactory?

Yes, the function passed to useFactory is only ever called once, on module instantiation just like useClass. Think about it — we are talking JavaScript here — its all just functions. And it seems logical now but at the time I thought:
useClass will instantiate a class (I was thinking .net classes…)
useFactory will get called for each dependent (in the example below both app.component and hello.component share the Constructable dependency but it only ever gets instantiated once. (I’ve used abstract class Constructable because it leaves a trail in the js — an empty function — while interface-s vanish after type-script compiler)

Look in the console — here on the bottom right corner. You can use the ^ key to pull it up.

--

--

Georgi Parlakov
ng-gotchas

Angular and DotNet dev. RxJs explorer. Testing proponent. A dad. Educative.io course author. https://gparlakov.github.io/