1

The address of the code is https://github.com/AngularClass/angular2-webpack-starter/blob/master/src/app/home/home.component.ts

import { AppState } from '../app.service';
import { Title } from './title';
import { XLargeDirective } from './x-large';

@Component({

  selector: 'home',

  providers: [
    Title
  ],

  styleUrls: [ './home.component.css' ],

  templateUrl: './home.component.html'
})
export class HomeComponent implements OnInit {

  public localState = { value: '' };

  constructor(
    public appState: AppState,
    public title: Title
  ) {}
.....
wangd933
  • 33
  • 5

1 Answers1

1

Because they provide it at the module level

https://github.com/AngularClass/angular2-webpack-starter/blob/master/src/app/app.module.ts#L38

https://github.com/AngularClass/angular2-webpack-starter/blob/master/src/app/app.module.ts#L67

Angulars DI is hierarchical. When a component has a dependency, DI looks at the components providers, it's parent components providers up to AppComponent and then continues at providers provided at module level, where it will find AppState.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567