injection in a Dart singleton with angular2 injection in a Dart singleton with angular2 dart dart

injection in a Dart singleton with angular2


In my opinion this is the wrong approach in Angular. Angular DI provides singletons by itself.

class Singleton {  final LoggerService log;  Singleton(this.log);}
bootstrap(AppComponent, [LoggerService, Singleton]);

should do what you want. As long as you don't add Singleton to providers elsewhere (for example on a component) Angular2 will always inject the same instance.

If you still want to keep above pattern, use

bootstrap(AppComponent, [    LoggerService,     provide(Singleton,         useFactory: (log) => new Singleton(log),         deps: [LoggerService])]);