Every angular provider is a singleton
#ng-gotchas #3
Are they really? provide: useClass
, useValue
, useExisting
sure, 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)