Controlling service instance creation and injection with NgModule
In a stark departure from Angular 1.x, Angular 2 features a hierarchical injection scheme. This has a substantial number of implications, and one of the more prominent one is the ability to control when, and how many, services are created.
Note
The code, links, and a live example of this are available at http://ngcookbook.herokuapp.com/2102/.
Getting ready
Suppose you begin with the following simple application:
[app/root.component.ts] import {Component} from '@angular/core'; @Component({ selector: 'root', template: ` <h1>root component!</h1> <article></article> <article></article> ` }) export class RootComponent {} [app/article.component.ts] import {Component} from '@angular/core'; @Component({ selector: 'article', template: ` <p>Article...