What is the difference between this:
bootstrap(AppComponent, [HeroService]);
And this:
@Component({ selector: 'test', providers: [HeroService] })
If you add it only to bootstrap(), you will get a singleton, if you add it to a component, you will get a new instance for each instance of the component. If you add it to both, it depends on where you enter it. Angular2 has a hierarchical DI. He is looking up for the provider, the first one returned is returned.
bootstrap()
See also http://blog.thoughtram.io/angular/2015/05/18/dependency-injection-in-angular-2.html
HeroService bootstrap, HeroService - , .
HeroService
bootstrap
providers .
providers
Angular 2.
, . , .
, .
Source: https://habr.com/ru/post/1624672/More articles:What face and mood should I use in the Gherkin / Specflow Given / When / Then instructions? - bddBeans initialization map using Spring JavaConfig - javaHow to prevent mutation of an object / array - javascriptHow to replace pthread_create during connection - chttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1624671/bad-symbolic-reference-a-signature-in-webuiclass-refers-to-term-eclipse-in-package-org-which-is-not-available&usg=ALkJrhhpqPH9w-6xmdlFCjTdEs9heNhMbwCannot find another supporting object, Async pipe and ngFor problems in angular2 - angularPython: converting a tuple to a 2D array - pythonKeep working with the perl script - perlLive from GoPro 4 to Android - androidChanging the color of a specific tick in ggplot2 - rAll Articles