diff --git a/CHANGELOG.md b/CHANGELOG.md index a9783fd..0977e2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/bootstrap.ts b/bootstrap.ts index f32464d..65c8267 100644 --- a/bootstrap.ts +++ b/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