Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Benjamin RICHARD
committed
Aug 8, 2016
1 parent
4eaae65
commit 1f57c2b
Showing
2 changed files
with
17 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |