Skip to content

Commit

Permalink
Chapter 9.4 syntax 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin RICHARD committed Aug 8, 2016
1 parent 4eaae65 commit 1f57c2b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,13 @@

### Fixed

## 2016-08-01 chapter-9.4.1 (012-papilusion)

### Added

Chapter 9 : Dependancy injection
* 9.4 : Other provider (syntax 1)

## 2016-08-01 chapter-9.3 (011-chrysacier)

### Added
Expand Down
20 changes: 10 additions & 10 deletions bootstrap.ts
@@ -1,25 +1,25 @@
import {bootstrap} from '@angular/platform-browser-dynamic';
import {PonyRacerAppComponent} from './ponyracer-app.component';
import {HTTP_PROVIDERS} from '@angular/http';
import {HTTP_PROVIDERS, Http} from '@angular/http';
import {RaceService} from './race.service';
import {FakeRaceService} from './fake.race.service';

function playWithInjector(inj) {
console.log(inj.get(RaceService));
console.log("inj.get(RaceService)", inj.get(RaceService));
// logs "RaceService {http: Http}"
console.log(inj.get('RaceServiceToken'));
// logs "RaceService {http: Http}" again
console.log(inj.get(RaceService) === inj.get(RaceService));
console.log("inj.get(RaceService) === inj.get(RaceService)", inj.get(RaceService) === inj.get(RaceService));
// logs "true", as the same instance is returned every time for a token
console.log(inj.get(RaceService) === inj.get('RaceServiceToken'));
// logs "false", as the providers are different,
// so there are two distinct instances
}

const IS_PROD = false;

bootstrap(PonyRacerAppComponent, [
HTTP_PROVIDERS,
{ provide: RaceService, useClass: FakeRaceService }
{ provide: RaceService,
useFactory: () => IS_PROD ? new RaceService(null) : new FakeRaceService(),
// array with the dependencies needed (for RceService here)
deps: [Http]
}
]).then(
// and play with the returned injector
appRef => playWithInjector(appRef.injector)
).catch(err => console.error(err)); // useful to catch the errors

0 comments on commit 1f57c2b

Please sign in to comment.