[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 injectable.

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 injectableclass DemoServicethat we want to inject into the DemoComponent.

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 DemoServiceinto the DemoComponentclass.

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.

The _demoService is now available inside the class via the this property. On line 17 we can see that the property this.itemsis set to this._demoService.

Dependency Injection & Providers

todo: fix & continue

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.