[DRAFT] NG2 bits: Angular Providers and Dependency Injection
Angular 2 Providers enables Dependency Injection (DI).
We can inject classes into a
Componentprovided that they are marked as a
Dependency Injection is declared the
@Componenttag, which is a function that takes an object as only argument. Let’s take the example of an of the
DemoServicethat we want to inject into the
On line 5 we define the
providersproperty of the component — an array that takes the collection of Dependency Injection tokens — tell Angular to inject an instance of the
On line 13 the private (to the class) variable
_demoServiceis create and assign to an instance of the
DemoServiceclass. Angular will provide the singleton* of the DemoService class.
_demoService is now available inside the class via the this property. On line 17 we can see that the property
this.itemsis set to
todo: fix & continue