diff --git a/public/docs/_examples/dependency-injection/e2e-spec.js b/public/docs/_examples/dependency-injection/e2e-spec.js new file mode 100644 index 0000000000..e5e146c158 --- /dev/null +++ b/public/docs/_examples/dependency-injection/e2e-spec.js @@ -0,0 +1,213 @@ + +describe('Dependency Injection Tests', function () { + + + var expectedMsg; + + beforeAll(function () { + browser.get(''); + }); + + describe('Cars:', function() { + + it('DI car displays as expected', function () { + expectedMsg = 'DI car with 4 cylinders and Flintstone tires.'; + expect(element(by.css('#di')).getText()).toEqual(expectedMsg); + }); + + it('No DI car displays as expected', function () { + expectedMsg = 'No DI car with 4 cylinders and Flintstone tires.'; + expect(element(by.css('#nodi')).getText()).toEqual(expectedMsg); + }); + + it('Injector car displays as expected', function () { + expectedMsg = 'Injector car with 4 cylinders and Flintstone tires.'; + expect(element(by.css('#injector')).getText()).toEqual(expectedMsg); + }); + + it('Factory car displays as expected', function () { + expectedMsg = 'Factory car with 4 cylinders and Flintstone tires.'; + expect(element(by.css('#factory')).getText()).toEqual(expectedMsg); + }); + + it('Simple car displays as expected', function () { + expectedMsg = 'Simple car with 4 cylinders and Flintstone tires.'; + expect(element(by.css('#simple')).getText()).toEqual(expectedMsg); + }); + + it('Super car displays as expected', function () { + expectedMsg = 'Super car with 12 cylinders and Flintstone tires.'; + expect(element(by.css('#super')).getText()).toEqual(expectedMsg); + }); + + it('Test car displays as expected', function () { + expectedMsg = 'Test car with 8 cylinders and YokoGoodStone tires.'; + expect(element(by.css('#test')).getText()).toEqual(expectedMsg); + }); + }); + + describe('Other Injections:', function() { + it('DI car displays as expected', function () { + expectedMsg = 'DI car with 4 cylinders and Flintstone tires.'; + expect(element(by.css('#car')).getText()).toEqual(expectedMsg); + }); + + it('Hero displays as expected', function () { + expectedMsg = 'Mr. Nice'; + expect(element(by.css('#hero')).getText()).toEqual(expectedMsg); + }); + + it('Optional injection displays as expected', function () { + expectedMsg = 'R.O.U.S.\'s? I don\'t think they exist!'; + expect(element(by.css('#rodent')).getText()).toEqual(expectedMsg); + }); + }); + + describe('Tests:', function() { + + it('Tests display as expected', function () { + expectedMsg = /Tests passed/; + expect(element(by.css('#tests')).getText()).toMatch(expectedMsg); + }); + + }); + + describe('Provider variations:', function() { + + it('P1 (class) displays as expected', function () { + expectedMsg = 'Hello from logger provided with Logger class'; + expect(element(by.css('#p1')).getText()).toEqual(expectedMsg); + }); + + it('P2 (Provider) displays as expected', function () { + expectedMsg = 'Hello from logger provided with Provider class and useClass'; + expect(element(by.css('#p2')).getText()).toEqual(expectedMsg); + }); + + it('P3 (provide) displays as expected', function () { + expectedMsg = 'Hello from logger provided with useClass'; + expect(element(by.css('#p3')).getText()).toEqual(expectedMsg); + }); + + it('P4 (useClass:BetterLogger) displays as expected', function () { + expectedMsg = 'Hello from logger provided with useClass:BetterLogger'; + expect(element(by.css('#p4')).getText()).toEqual(expectedMsg); + }); + + it('P5 (useClass:EvenBetterLogger - dependency) displays as expected', function () { + expectedMsg = 'Message to Bob: Hello from EvenBetterlogger.'; + expect(element(by.css('#p5')).getText()).toEqual(expectedMsg); + }); + + it('P6a (no alias) displays as expected', function () { + expectedMsg = 'Hello OldLogger (but we want NewLogger)'; + expect(element(by.css('#p6a')).getText()).toEqual(expectedMsg); + }); + + it('P6b (alias) displays as expected', function () { + expectedMsg = 'Hello from NewLogger (via aliased OldLogger)'; + expect(element(by.css('#p6b')).getText()).toEqual(expectedMsg); + }); + + it('P7 (useValue) displays as expected', function () { + expectedMsg = 'Silent logger says "Shhhhh!". Provided via "useValue"'; + expect(element(by.css('#p7')).getText()).toEqual(expectedMsg); + }); + + it('P8 (useFactory) displays as expected', function () { + expectedMsg = 'Hero service injected successfully'; + expect(element(by.css('#p8')).getText()).toEqual(expectedMsg); + }); + + it('P9a (string token) displays as expected', function () { + expectedMsg = '"app.config" Application title is Dependency Injection'; + expect(element(by.css('#p9a')).getText()).toEqual(expectedMsg); + }); + + it('P9b (OpaqueToken) displays as expected', function () { + expectedMsg = 'APP_CONFIG Application title is Dependency Injection'; + expect(element(by.css('#p9b')).getText()).toEqual(expectedMsg); + }); + + it('P10a (required dependency) displays as expected', function () { + expectedMsg = 'Hello from the required logger.'; + expect(element(by.css('#p10a')).getText()).toEqual(expectedMsg); + }); + + it('P10b (optional dependency) displays as expected', function () { + expectedMsg = 'Optional logger was not available.'; + expect(element(by.css('#p10b')).getText()).toEqual(expectedMsg); + }) + }); + + describe('User/Heroes:', function() { + it('User is Bob - unauthorized', function () { + expectedMsg = /Bob, is not authorized/; + expect(element(by.css('#user')).getText()).toMatch(expectedMsg); + }); + + it('should have button', function () { + expect(element.all(by.cssContainingText('button','Next User')) + .get(0).isDisplayed()).toBe(true, "'Next User' button should be displayed"); + }); + + it('unauthorized user should have multiple unauthorized heroes', function () { + var heroes = element.all(by.css('#unauthorized hero-list div')); + expect(heroes.count()).toBeGreaterThan(0); + }); + + it('unauthorized user should have no secret heroes', function () { + var heroes = element.all(by.css('#unauthorized hero-list div')); + expect(heroes.count()).toBeGreaterThan(0); + + heroes.filter(function(elem, index){ + return elem.getText().then(function(text) { + return /secret/.test(text); + }); + }).then(function(filteredElements) { + //console.log("******Secret heroes count: "+filteredElements.length); + expect(filteredElements.length).toEqual(0); + }); + }); + + it('unauthorized user should have no authorized heros listed', function () { + expect(element.all(by.css('#authorized hero-list div')).count()).toEqual(0); + }); + + describe('after button click', function() { + + beforeAll(function (done) { + var buttonEle = element.all(by.cssContainingText('button','Next User')).get(0); + buttonEle.click().then(done,done); + }); + + it('User is Alice - authorized', function () { + expectedMsg = /Alice, is authorized/; + expect(element(by.css('#user')).getText()).toMatch(expectedMsg); + }); + + it('authorized user should have multiple authorized heroes ', function () { + var heroes = element.all(by.css('#authorized hero-list div')); + expect(heroes.count()).toBeGreaterThan(0); + }); + + it('authorized user should have secret heroes', function () { + var heroes = element.all(by.css('#authorized hero-list div')); + expect(heroes.count()).toBeGreaterThan(0); + + heroes.filter(function(elem, index){ + return elem.getText().then(function(text) { + return /secret/.test(text); + }); + }).then(function(filteredElements) { + //console.log("******Secret heroes count: "+filteredElements.length); + expect(filteredElements.length).toBeGreaterThan(0); + }); + }); + + it('authorized user should have no unauthorized heros listed', function () { + expect(element.all(by.css('#unauthorized hero-list div')).count()).toEqual(0); + }); + }); + }); +}); diff --git a/public/docs/_examples/dependency-injection/ts/.gitignore b/public/docs/_examples/dependency-injection/ts/.gitignore new file mode 100644 index 0000000000..2cb7d2a2e9 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/.gitignore @@ -0,0 +1 @@ +**/*.js diff --git a/public/docs/_examples/dependency-injection/ts/app/app.component.1.ts b/public/docs/_examples/dependency-injection/ts/app/app.component.1.ts new file mode 100644 index 0000000000..480765e870 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/app.component.1.ts @@ -0,0 +1,31 @@ +// Early versions + +// #docregion +import {Component} from 'angular2/core'; +import {CarComponent} from './car/car.component'; +import {HeroesComponent} from './heroes/heroes.component.1'; + +@Component({ + selector: 'my-app', + template: ` +

{{title}}

+ + + `, + directives:[CarComponent, HeroesComponent] +}) + +export class AppComponent { + title = 'Dependency Injection'; +} +// #enddocregion + + +/* +//#docregion ctor-di-fail +// FAIL! Injectable `config` is not a class! +constructor(heroService: HeroService, config: config) { + this.title = config.title; +} +//#enddocregion ctor-di-fail +*/ diff --git a/public/docs/_examples/dependency-injection/ts/app/app.component.2.ts b/public/docs/_examples/dependency-injection/ts/app/app.component.2.ts new file mode 100644 index 0000000000..e935f4c6e1 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/app.component.2.ts @@ -0,0 +1,39 @@ +// #docregion +// #docregion imports +import {Component} from 'angular2/core'; +import {CarComponent} from './car/car.component'; +import {HeroesComponent} from './heroes/heroes.component.1'; + +import {provide, Inject} from 'angular2/core'; +import {Config, CONFIG} from './app.config'; +import {Logger} from './logger.service'; +// #enddocregion imports + +@Component({ + selector: 'my-app', + template: ` +

{{title}}

+ + + `, + directives:[CarComponent, HeroesComponent], +// #docregion providers + providers: [ + Logger, + // #docregion provider-config + provide('app.config', {useValue: CONFIG}) + // #enddocregion provider-config + ] +// #docregion providers +}) +export class AppComponent { + title:string; + + // #docregion ctor + constructor(@Inject('app.config') config:Config) { + + this.title = config.title; + } + // #docregion ctor +} +// #enddocregion diff --git a/public/docs/_examples/dependency-injection/ts/app/app.component.ts b/public/docs/_examples/dependency-injection/ts/app/app.component.ts new file mode 100644 index 0000000000..ef81e0630c --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/app.component.ts @@ -0,0 +1,65 @@ +// #docplaster +// #docregion +// #docregion imports +import {Component, Inject, provide} from 'angular2/core'; + +import {CarComponent} from './car/car.component'; +import {HeroesComponent} from './heroes/heroes.component'; + +import {APP_CONFIG, + Config, CONFIG} from './app.config'; +import {Logger} from './logger.service'; + +import {User, UserService} from './user.service'; +// #enddocregion imports +import {InjectorComponent} from './injector.component'; +import {TestComponent} from './test.component'; +import {ProvidersComponent} from './providers.component'; + +@Component({ + selector: 'my-app', + template: ` +

{{title}}

+ + + +

User

+

+ {{userInfo}} + +

+ + + `, + directives:[CarComponent, HeroesComponent, + InjectorComponent, TestComponent, ProvidersComponent], +// #docregion providers + providers: [ + Logger, + UserService, + provide(APP_CONFIG, {useValue: CONFIG}) + ] +// #enddocregion providers +}) +export class AppComponent { + title:string; + + //#docregion ctor + constructor( + @Inject(APP_CONFIG) config:Config, + private _userService: UserService) { + + this.title = config.title; + } + // #enddocregion ctor + + get isAuthorized() { return this.user.isAuthorized;} + nextUser() { this._userService.getNewUser(); } + get user() { return this._userService.user; } + + get userInfo() { + return `Current user, ${this.user.name}, is `+ + `${this.isAuthorized ? '' : 'not'} authorized. `; + } +} +// #enddocregion diff --git a/public/docs/_examples/dependency-injection/ts/app/app.config.ts b/public/docs/_examples/dependency-injection/ts/app/app.config.ts new file mode 100644 index 0000000000..39f304d31a --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/app.config.ts @@ -0,0 +1,18 @@ +//#docregion +// #docregion token +import {OpaqueToken} from 'angular2/core'; + +export let APP_CONFIG = new OpaqueToken('app.config'); +// #enddocregion token + +//#docregion config +export interface Config { + apiEndpoint: string, + title: string +} + +export const CONFIG:Config = { + apiEndpoint: 'api.heroes.com', + title: 'Dependency Injection' +}; +//#enddocregion config \ No newline at end of file diff --git a/public/docs/_examples/dependency-injection/ts/app/car/car-creations.ts b/public/docs/_examples/dependency-injection/ts/app/car/car-creations.ts new file mode 100644 index 0000000000..b6e56b85eb --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/car/car-creations.ts @@ -0,0 +1,46 @@ +// Examples with car and engine variations + +// #docplaster +import {Car, Engine, Tires} from './car'; + +///////// example 1 //////////// +export function simpleCar() { + //#docregion car-ctor-instantiation + // Simple car with 4 cylinders and Flintstone tires. + var car = new Car(new Engine(), new Tires()); + //#enddocregion car-ctor-instantiation + car.description = 'Simple'; + return car; +} + + +///////// example 2 //////////// +//#docregion car-ctor-instantiation-with-param + class Engine2 { + constructor(public cylinders: number) { } + } +//#enddocregion car-ctor-instantiation-with-param +export function superCar() { +//#docregion car-ctor-instantiation-with-param + // Super car with 12 cylinders and Flintstone tires. + var bigCylinders = 12; + var car = new Car(new Engine2(bigCylinders), new Tires()); +//#enddocregion car-ctor-instantiation-with-param + car.description = 'Super'; + return car; +} + +/////////// example 3 ////////// + //#docregion car-ctor-instantiation-with-mocks + class MockEngine extends Engine { cylinders = 8; } + class MockTires extends Tires { make = "YokoGoodStone"; } + + //#enddocregion car-ctor-instantiation-with-mocks +export function testCar() { + //#docregion car-ctor-instantiation-with-mocks + // Test car with 8 cylinders and YokoGoodStone tires. + var car = new Car(new MockEngine(), new MockTires()); + //#enddocregion car-ctor-instantiation-with-mocks + car.description = 'Test'; + return car; +} diff --git a/public/docs/_examples/dependency-injection/ts/app/car/car-factory.ts b/public/docs/_examples/dependency-injection/ts/app/car/car-factory.ts new file mode 100644 index 0000000000..2922206748 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/car/car-factory.ts @@ -0,0 +1,19 @@ +// #docregion +import {Engine, Tires, Car} from './car'; + +export class CarFactory { + + createCar() { + let car = new Car(this.createEngine(), this.createTires()); + car.description = 'Factory'; + return car; + } + + createEngine() { + return new Engine(); + } + + createTires() { + return new Tires(); + } +} diff --git a/public/docs/_examples/dependency-injection/ts/app/car/car-injector.ts b/public/docs/_examples/dependency-injection/ts/app/car/car-injector.ts new file mode 100644 index 0000000000..a94202585c --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/car/car-injector.ts @@ -0,0 +1,37 @@ +// #docplaster +//#docregion +import { Injector } from 'angular2/core'; + +import {Car, Engine, Tires} from './car'; +import {Logger} from '../logger.service'; + +//#docregion injector +export function useInjector() { + var injector:Injector; + +//#enddocregion injector +/* +//#docregion injector-no-new + // Cannot 'new' an Injector like this! + var injector = new Injector([Car, Engine, Tires, Logger]); +//#enddocregion injector-no-new +*/ + +//#docregion injector + //#docregion injector-create-and-call + injector = Injector.resolveAndCreate([Car, Engine, Tires, Logger]); + //#docregion injector-call + var car = injector.get(Car); + //#enddocregion injector-call + //#enddocregion injector-create-and-call + car.description = 'Injector'; + + var logger = injector.get(Logger); + logger.log('Injector car.drive() said: '+car.drive()); + + return car; +} +//#enddocregion injector + + +//#enddocregion diff --git a/public/docs/_examples/dependency-injection/ts/app/car/car-no-di.ts b/public/docs/_examples/dependency-injection/ts/app/car/car-no-di.ts new file mode 100644 index 0000000000..9556bffcab --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/car/car-no-di.ts @@ -0,0 +1,24 @@ +// Car without DI +import {Engine, Tires} from './car'; + +//#docregion car +export class Car { + + //#docregion car-ctor + public engine: Engine; + public tires: Tires; + public description = 'No DI'; + + constructor() { + this.engine = new Engine(); + this.tires = new Tires(); + } + //#enddocregion car-ctor + + // Method using the engine and tires + drive() { + return `${this.description} car with ` + + `${this.engine.cylinders} cylinders and ${this.tires.make} tires.` + } +} +//#enddocregion car \ No newline at end of file diff --git a/public/docs/_examples/dependency-injection/ts/app/car/car.component.ts b/public/docs/_examples/dependency-injection/ts/app/car/car.component.ts new file mode 100644 index 0000000000..c66e0fbcce --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/car/car.component.ts @@ -0,0 +1,37 @@ +// #docregion +import { Component, Injector} from 'angular2/core'; +import { Car, Engine, Tires } from './car'; +import { Car as CarNoDi } from './car-no-di'; +import { CarFactory} from './car-factory'; + +import { testCar, + simpleCar, + superCar } from './car-creations'; + +import { useInjector } from './car-injector'; + + +@Component({ + selector: 'my-car', + template: ` +

Cars

+
{{car.drive()}}
+
{{noDiCar.drive()}}
+
{{injectorCar.drive()}}
+
{{factoryCar.drive()}}
+
{{simpleCar.drive()}}
+
{{superCar.drive()}}
+
{{testCar.drive()}}
+ `, + providers: [Car, Engine, Tires] +}) +export class CarComponent { + constructor(public car: Car) {} + + factoryCar = (new CarFactory).createCar(); + injectorCar = useInjector(); + noDiCar = new CarNoDi; + simpleCar = simpleCar(); + superCar = superCar(); + testCar = testCar(); +} diff --git a/public/docs/_examples/dependency-injection/ts/app/car/car.ts b/public/docs/_examples/dependency-injection/ts/app/car/car.ts new file mode 100644 index 0000000000..e80483ed55 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/car/car.ts @@ -0,0 +1,32 @@ +// #docregion +import {Injectable} from 'angular2/core'; + +// #docregion engine +export class Engine { + public cylinders = 4; // default +} +// #enddocregion engine + +// #docregion tires +export class Tires { + public make = 'Flintstone'; + public model = 'Square'; +} +// #enddocregion tires + +@Injectable() +// #docregion car +export class Car { + //#docregion car-ctor + public description = 'DI'; + + constructor(public engine: Engine, public tires: Tires) { } + // #enddocregion car-ctor + + // Method using the engine and tires + drive() { + return `${this.description} car with ` + + `${this.engine.cylinders} cylinders and ${this.tires.make} tires.` + } +} +// #enddocregion car diff --git a/public/docs/_examples/dependency-injection/ts/app/heroes/hero-list.component.1.ts b/public/docs/_examples/dependency-injection/ts/app/heroes/hero-list.component.1.ts new file mode 100644 index 0000000000..f82ced305d --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/heroes/hero-list.component.1.ts @@ -0,0 +1,16 @@ +// #docregion +import { Component } from 'angular2/core'; +import { Hero } from './hero'; +import { HEROES } from './mock-heroes'; + +@Component({ + selector: 'hero-list', + template: ` +
+ {{hero.id}} - {{hero.name}} +
+ `, +}) +export class HeroListComponent { + heroes = HEROES; +} diff --git a/public/docs/_examples/dependency-injection/ts/app/heroes/hero-list.component.2.ts b/public/docs/_examples/dependency-injection/ts/app/heroes/hero-list.component.2.ts new file mode 100644 index 0000000000..4c78e6a190 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/heroes/hero-list.component.2.ts @@ -0,0 +1,22 @@ +// #docregion +import { Component } from 'angular2/core'; +import { Hero } from './hero'; +import { HeroService } from './hero.service'; + +@Component({ + selector: 'hero-list', + template: ` +
+ {{hero.id}} - {{hero.name}} +
+ `, +}) +export class HeroListComponent { + heroes: Hero[]; + + //#docregion ctor + constructor(heroService: HeroService) { + this.heroes = heroService.getHeroes(); + } + //#enddocregion ctor +} diff --git a/public/docs/_examples/dependency-injection/ts/app/heroes/hero-list.component.ts b/public/docs/_examples/dependency-injection/ts/app/heroes/hero-list.component.ts new file mode 100644 index 0000000000..b244d6d38c --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/heroes/hero-list.component.ts @@ -0,0 +1,23 @@ +// #docregion +import { Component } from 'angular2/core'; +import { Hero } from './hero'; +import { HeroService } from './hero.service'; + +@Component({ + selector: 'hero-list', + template: ` +
+ {{hero.id}} - {{hero.name}} + ({{hero.isSecret ? 'secret' : 'public'}}) +
+ `, +}) +export class HeroListComponent { + heroes: Hero[]; + + //#docregion ctor-signature + constructor(heroService: HeroService) { + //#enddocregion ctor-signature + this.heroes = heroService.getHeroes(); + } +} diff --git a/public/docs/_examples/dependency-injection/ts/app/heroes/hero.service.1.ts b/public/docs/_examples/dependency-injection/ts/app/heroes/hero.service.1.ts new file mode 100644 index 0000000000..ae962bd9b6 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/heroes/hero.service.1.ts @@ -0,0 +1,7 @@ +// #docregion +import {Hero} from './hero'; +import {HEROES} from './mock-heroes'; + +export class HeroService { + getHeroes() { return HEROES; } +} diff --git a/public/docs/_examples/dependency-injection/ts/app/heroes/hero.service.2.ts b/public/docs/_examples/dependency-injection/ts/app/heroes/hero.service.2.ts new file mode 100644 index 0000000000..7060373fbf --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/heroes/hero.service.2.ts @@ -0,0 +1,16 @@ +// #docregion +import {Injectable} from 'angular2/core'; +import {Hero} from './hero'; +import {HEROES} from './mock-heroes'; +import {Logger} from '../logger.service'; + +@Injectable() +export class HeroService { + + constructor(private _logger: Logger) { } + + getHeroes() { + this._logger.log('Getting heroes ...') + return HEROES; + } +} diff --git a/public/docs/_examples/dependency-injection/ts/app/heroes/hero.service.provider.ts b/public/docs/_examples/dependency-injection/ts/app/heroes/hero.service.provider.ts new file mode 100644 index 0000000000..9f259df0da --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/heroes/hero.service.provider.ts @@ -0,0 +1,19 @@ +// #docregion +import {provide} from 'angular2/core'; +import {HeroService} from './hero.service'; +import {Logger} from '../logger.service'; +import {UserService} from '../user.service'; + +// #docregion factory +let heroServiceFactory = (logger: Logger, userService: UserService) => { + return new HeroService(logger, userService.user.isAuthorized); +} +// #enddocregion factory + +// #docregion provider +export let heroServiceProvider = + provide(HeroService, { + useFactory: heroServiceFactory, + deps: [Logger, UserService] + }); +// #enddocregion provider diff --git a/public/docs/_examples/dependency-injection/ts/app/heroes/hero.service.ts b/public/docs/_examples/dependency-injection/ts/app/heroes/hero.service.ts new file mode 100644 index 0000000000..0eaf72848b --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/heroes/hero.service.ts @@ -0,0 +1,22 @@ +// #docregion +import {Injectable} from 'angular2/core'; +import {Hero} from './hero'; +import {HEROES} from './mock-heroes'; +import {Logger} from '../logger.service'; + +@Injectable() +export class HeroService { + private _user:string; + + // #docregion internals + constructor( + private _logger: Logger, + private _isAuthorized: boolean) { } + + getHeroes() { + let auth = this._isAuthorized ? 'authorized ': 'unauthorized'; + this._logger.log(`Getting heroes for ${auth} user.`); + return HEROES.filter(hero => this._isAuthorized || !hero.isSecret); + } + // #enddocregion internals +} diff --git a/public/docs/_examples/dependency-injection/ts/app/heroes/hero.ts b/public/docs/_examples/dependency-injection/ts/app/heroes/hero.ts new file mode 100644 index 0000000000..5c49328241 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/heroes/hero.ts @@ -0,0 +1,6 @@ +// #docregion +export class Hero { + id: number; + name: string; + isSecret = false; +} diff --git a/public/docs/_examples/dependency-injection/ts/app/heroes/heroes.component.1.ts b/public/docs/_examples/dependency-injection/ts/app/heroes/heroes.component.1.ts new file mode 100644 index 0000000000..86b8520140 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/heroes/heroes.component.1.ts @@ -0,0 +1,24 @@ +// #docplaster +// #docregion +// #docregion v1 +import { Component } from 'angular2/core'; +import { HeroListComponent } from './hero-list.component'; +// #enddocregion v1 +import { HeroService } from './hero.service'; +// #docregion v1 + +@Component({ + selector: 'my-heroes', + template: ` +

Heroes

+ + `, + // #enddocregion v1 + // #docregion providers + providers:[HeroService], + // #enddocregion providers +// #docregion v1 + directives:[HeroListComponent] +}) +export class HeroesComponent { } +// #enddocregion v1 diff --git a/public/docs/_examples/dependency-injection/ts/app/heroes/heroes.component.ts b/public/docs/_examples/dependency-injection/ts/app/heroes/heroes.component.ts new file mode 100644 index 0000000000..e7b1bd741d --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/heroes/heroes.component.ts @@ -0,0 +1,15 @@ +// #docregion +import { Component } from 'angular2/core'; +import { HeroListComponent } from './hero-list.component'; +import { heroServiceProvider} from './hero.service.provider'; + +@Component({ + selector: 'my-heroes', + template: ` +

Heroes

+ + `, + providers:[heroServiceProvider], + directives:[HeroListComponent] +}) +export class HeroesComponent { } diff --git a/public/docs/_examples/dependency-injection/ts/app/heroes/mock-heroes.ts b/public/docs/_examples/dependency-injection/ts/app/heroes/mock-heroes.ts new file mode 100644 index 0000000000..83726eb2f3 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/heroes/mock-heroes.ts @@ -0,0 +1,15 @@ +// #docregion +import { Hero } from './hero'; + +export var HEROES: Hero[] = [ + { "id": 11, isSecret: false, "name": "Mr. Nice" }, + { "id": 12, isSecret: false, "name": "Narco" }, + { "id": 13, isSecret: false, "name": "Bombasto" }, + { "id": 14, isSecret: false, "name": "Celeritas" }, + { "id": 15, isSecret: false, "name": "Magneta" }, + { "id": 16, isSecret: false, "name": "RubberMan" }, + { "id": 17, isSecret: false, "name": "Dynama" }, + { "id": 18, isSecret: true, "name": "Dr IQ" }, + { "id": 19, isSecret: true, "name": "Magma" }, + { "id": 20, isSecret: true, "name": "Tornado" } +]; diff --git a/public/docs/_examples/dependency-injection/ts/app/injector.component.ts b/public/docs/_examples/dependency-injection/ts/app/injector.component.ts new file mode 100644 index 0000000000..300eaee9d3 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/injector.component.ts @@ -0,0 +1,46 @@ +// #docplaster +//#docregion +import {Component, Injector} from 'angular2/core'; + +import {Car, Engine, Tires} from './car/car'; +import {HeroService} from './heroes/hero.service'; +import {heroServiceProvider} from './heroes/hero.service.provider'; +import {Logger} from './logger.service'; + +//#docregion injector +@Component({ + selector: 'my-injectors', + template: ` +

Other Injections

+
{{car.drive()}}
+
{{hero.name}}
+
{{rodent}}
+ `, + providers: [Car, Engine, Tires, + heroServiceProvider, Logger] +}) +export class InjectorComponent { + constructor(private _injector: Injector) { } + + car:Car = this._injector.get(Car); + + //#docregion get-hero-service + heroService:HeroService = this._injector.get(HeroService); + //#enddocregion get-hero-service + hero = this.heroService.getHeroes()[0]; + + get rodent() { + let rous = this._injector.getOptional(ROUS); + if (rous) { + throw new Error('Aaaargh!') + } + return "R.O.U.S.'s? I don't think they exist!"; + } +} +//#enddocregion injector + +/** + * R.O.U.S. - Rodents Of Unusual Size + * // https://www.youtube.com/watch?v=BOv5ZjAOpC8 + */ +class ROUS { } diff --git a/public/docs/_examples/dependency-injection/ts/app/logger.service.ts b/public/docs/_examples/dependency-injection/ts/app/logger.service.ts new file mode 100644 index 0000000000..4afbd04017 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/logger.service.ts @@ -0,0 +1,11 @@ +// #docregion +import {Injectable} from 'angular2/core'; + +@Injectable() +export class Logger { + logs:string[] = []; // capture logs for testing + log(message: string){ + this.logs.push(message); + console.log(message); + } +} diff --git a/public/docs/_examples/dependency-injection/ts/app/main.1.ts b/public/docs/_examples/dependency-injection/ts/app/main.1.ts new file mode 100644 index 0000000000..a21d401a51 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/main.1.ts @@ -0,0 +1,8 @@ +import {bootstrap} from 'angular2/platform/browser'; +import {AppComponent} from './app.component'; +import {HeroService} from './heroes/hero.service'; + +//#docregion bootstrap +// Injecting services in bootstrap works but is discouraged +bootstrap(AppComponent, [HeroService]); +//#enddocregion bootstrap diff --git a/public/docs/_examples/dependency-injection/ts/app/main.ts b/public/docs/_examples/dependency-injection/ts/app/main.ts new file mode 100644 index 0000000000..8470a28175 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/main.ts @@ -0,0 +1,9 @@ +//#docregion +import {bootstrap} from 'angular2/platform/browser'; +import {AppComponent} from './app.component'; +import {ProvidersComponent} from './providers.component'; + +//#docregion bootstrap +bootstrap(AppComponent); +//#enddocregion bootstrap +bootstrap(ProvidersComponent); \ No newline at end of file diff --git a/public/docs/_examples/dependency-injection/ts/app/providers.component.ts b/public/docs/_examples/dependency-injection/ts/app/providers.component.ts new file mode 100644 index 0000000000..ebc8bfe162 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/providers.component.ts @@ -0,0 +1,355 @@ +// Examples of provider arrays +import { Component, Host, Inject, Injectable, + provide, Provider} from 'angular2/core'; + +import { APP_CONFIG, + Config, CONFIG } from './app.config'; + +import { HeroService} from './heroes/hero.service'; +import { heroServiceProvider } from './heroes/hero.service.provider'; +import { Logger } from './logger.service'; +import { User, UserService } from './user.service'; + +let template = '{{log}}'; + +////////////////////////////////////////// +@Component({ + selector: 'provider-1', + template: template, + providers: + //#docregion providers-1 + [Logger] + //#enddocregion providers-1 +}) +export class ProviderComponent1 { + log:string; + constructor(logger: Logger) { + logger.log('Hello from logger provided with Logger class'); + this.log = logger.logs[0]; + } +} + +////////////////////////////////////////// +@Component({ + selector: 'provider-2', + template: template, + providers: + //#docregion providers-2 + [new Provider(Logger, {useClass: Logger})] + //#enddocregion providers-2 +}) +export class ProviderComponent2 { + log:string; + constructor(logger: Logger) { + logger.log('Hello from logger provided with Provider class and useClass'); + this.log = logger.logs[0]; + } +} + +////////////////////////////////////////// +@Component({ + selector: 'provider-3', + template: template, + providers: + //#docregion providers-3 + [provide(Logger, {useClass: Logger})] + //#enddocregion providers-3 +}) +export class ProviderComponent3 { + log:string; + constructor(logger: Logger) { + logger.log('Hello from logger provided with useClass'); + this.log = logger.logs[0]; + } +} + +////////////////////////////////////////// +class BetterLogger extends Logger {} + +@Component({ + selector: 'provider-4', + template: template, + providers: + //#docregion providers-4 + [provide(Logger, {useClass: BetterLogger})] + //#enddocregion providers-4 +}) +export class ProviderComponent4 { + log:string; + constructor(logger: Logger) { + logger.log('Hello from logger provided with useClass:BetterLogger'); + this.log = logger.logs[0]; + } +} + +////////////////////////////////////////// +// #docregion EvenBetterLogger +@Injectable() +class EvenBetterLogger { + logs:string[] = []; + + constructor(private _userService: UserService) { } + + log(message:string){ + message = `Message to ${this._userService.user.name}: ${message}.`; + console.log(message); + this.logs.push(message); + } +} +// #enddocregion EvenBetterLogger + +@Component({ + selector: 'provider-5', + template: template, + providers: + //#docregion providers-5 + [ + UserService, + provide(Logger, {useClass: EvenBetterLogger}) + ] + //#enddocregion providers-5 +}) +export class ProviderComponent5 { + log:string; + constructor(logger: Logger) { + logger.log('Hello from EvenBetterlogger'); + this.log = logger.logs[0]; + } +} + +////////////////////////////////////////// +class NewLogger extends Logger {} +class OldLogger { + logs:string[] = []; + log(message:string) { + throw new Error('Should not call the old logger!') + }; +} + +@Component({ + selector: 'provider-6a', + template: template, + providers: + //#docregion providers-6a + [ + NewLogger, + // Not aliased! Creates two instances of `NewLogger` + provide(OldLogger, {useClass:NewLogger}) + ] + //#enddocregion providers-6a +}) +export class ProviderComponent6a { + log:string; + constructor(newLogger:NewLogger, oldLogger: OldLogger) { + if (newLogger === oldLogger){ + throw new Error('expected the two loggers to be different instances'); + } + oldLogger.log('Hello OldLogger (but we want NewLogger)'); + // The newLogger wasn't called so no logs[] + // display the logs of the oldLogger. + this.log = newLogger.logs[0] || oldLogger.logs[0]; + } +} + +@Component({ + selector: 'provider-6b', + template: template, + providers: + //#docregion providers-6b + [ + NewLogger, + // Alias OldLogger w/ reference to NewLogger + provide(OldLogger, {useExisting: NewLogger}) + ] + //#enddocregion providers-6b +}) +export class ProviderComponent6b { + log:string; + constructor(newLogger:NewLogger, oldLogger: OldLogger) { + if (newLogger !== oldLogger){ + throw new Error('expected the two loggers to be the same instance'); + } + oldLogger.log('Hello from NewLogger (via aliased OldLogger)'); + this.log = newLogger.logs[0]; + } +} + +////////////////////////////////////////// +// #docregion silent-logger +// An object in the shape of the logger service +let silentLogger = { + logs: ['Silent logger says "Shhhhh!". Provided via "useValue"'], + log: () => {} +} +// #enddocregion silent-logger + +@Component({ + selector: 'provider-7', + template: template, + providers: + //#docregion providers-7 + [provide(Logger, {useValue: silentLogger})] + //#enddocregion providers-7 +}) +export class ProviderComponent7 { + log:string; + constructor(logger: Logger) { + logger.log('Hello from logger provided with useValue'); + this.log = logger.logs[0]; + } +} +///////////////// + +@Component({ + selector: 'provider-8', + template: template, + providers:[heroServiceProvider, Logger, UserService] +}) +export class ProviderComponent8{ + // #docregion provider-8-ctor + constructor(heroService: HeroService){ } + // #enddocregion provider-8-ctor + + // must be true else this component would have blown up at runtime + log = 'Hero service injected successfully' +} + +///////////////// +@Component({ + selector: 'provider-9a', + template: template, + providers: + /* + // #docregion providers-9a-interface + // FAIL! Can't use interface as provider token + [provide(Config, {useValue: CONFIG})] + // #enddocregion providers-9a-interface + */ + // #docregion providers-9a + // Use string as provider token + [provide('app.config', {useValue: CONFIG})] + // #enddocregion providers-9a +}) +export class ProviderComponent9a { + log: string; + /* + // #docregion provider-9a-ctor-interface + // FAIL! Can't inject using the interface as the parameter type + constructor(private _config: Config){ } + // #enddocregion provider-9a-ctor-interface + */ + + // #docregion provider-9a-ctor + // @Inject(token) to inject the dependency + constructor(@Inject('app.config') private _config: Config){ } + // #enddocregion provider-9a-ctor + + ngOnInit() { + this.log = '"app.config" Application title is ' + this._config.title; + } +} + +@Component({ + selector: 'provider-9b', + template: template, + // #docregion providers-9b + providers:[provide(APP_CONFIG, {useValue: CONFIG})] + // #enddocregion providers-9b +}) +export class ProviderComponent9b { + log: string; + // #docregion provider-9b-ctor + constructor(@Inject(APP_CONFIG) private _config: Config){ } + // #enddocregion provider-9b-ctor + + ngOnInit() { + this.log = 'APP_CONFIG Application title is ' + this._config.title; + } +} +////////////////////////////////////////// +// Normal required logger +@Component({ + selector: 'provider-10a', + template: template, + //#docregion providers-logger + providers: [Logger] + //#enddocregion providers-logger +}) +export class ProviderComponent10a { + log:string; + constructor(logger: Logger) { + logger.log('Hello from the required logger.'); + this.log = logger.logs[0]; + } +} + +// Optional logger +// #docregion import-optional +import {Optional} from 'angular2/core'; +// #enddocregion import-optional + +@Component({ + selector: 'provider-10b', + template: template +}) +export class ProviderComponent10b { + // #docregion provider-10-ctor + log:string; + constructor(@Optional() private _logger:Logger) { } + // #enddocregion provider-10-ctor + + ngOnInit() { + // #docregion provider-10-logger + // No logger? Make one! + if (!this._logger) { + this._logger = { + log: (msg:string)=> this._logger.logs.push(msg), + logs: [] + } + this._logger.log("Optional logger was not available.") + } + // #enddocregion provider-10-logger + else { + this._logger.log('Hello from the injected logger.') + this.log = this._logger.logs[0]; + } + this.log = this._logger.logs[0]; + } +} + +///////////////// +@Component({ + selector: 'my-providers', + template: ` +

Provider variations

+
+
+
+
+
+
+
+
+
+
+
+
+
+ `, + directives:[ + ProviderComponent1, + ProviderComponent2, + ProviderComponent3, + ProviderComponent4, + ProviderComponent5, + ProviderComponent6a, + ProviderComponent6b, + ProviderComponent7, + ProviderComponent8, + ProviderComponent9a, + ProviderComponent9b, + ProviderComponent10a, + ProviderComponent10b, + ], +}) +export class ProvidersComponent { } \ No newline at end of file diff --git a/public/docs/_examples/dependency-injection/ts/app/test.component.ts b/public/docs/_examples/dependency-injection/ts/app/test.component.ts new file mode 100644 index 0000000000..6499edec65 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/test.component.ts @@ -0,0 +1,53 @@ +// Simulate a simple test +// Reader should look to the testing chapter for the real thing + +import {Component} from 'angular2/core'; +import { HeroService } from './heroes/hero.service'; +import { HeroListComponent } from './heroes/hero-list.component'; + +@Component({ + selector: 'my-tests', + template: ` +

Tests

+

Tests {{results.pass}}: {{results.message}}

+ ` +}) +export class TestComponent { + results = runTests(); +} + +///////////////////////////////////// +function runTests() { + + //#docregion spec + let expectedHeroes = [{name: 'A'}, {name: 'B'}] + let mockService = {getHeroes: () => expectedHeroes } + + it("should have heroes when HeroListComponent created", () => { + let hlc = new HeroListComponent(mockService); + expect(hlc.heroes.length).toEqual(expectedHeroes.length); + }) + //#enddocregion spec + + return testResults; +} + +////////////////////////////////// +// Fake Jasmine infrastructure +var testName:string; +var testResults: {pass:string; message:string}; + +function expect(actual:any) { + return { + toEqual: function(expected:any){ + testResults = actual === expected? + {pass:'passed', message: `${testName}`} : + {pass:'failed', message: `${testName}; expected ${actual} to equal ${expected}.`}; + } + } +} + +function it(label:string, test: () => void) { + testName = label; + test(); +} \ No newline at end of file diff --git a/public/docs/_examples/dependency-injection/ts/app/user.service.ts b/public/docs/_examples/dependency-injection/ts/app/user.service.ts new file mode 100644 index 0000000000..1fd5b46516 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/app/user.service.ts @@ -0,0 +1,23 @@ +// #docregion +import {Injectable} from 'angular2/core'; + +@Injectable() +export class UserService { + // Todo: get the user; don't 'new' it. + private _alice = new User('Alice', true); + private _bob = new User('Bob', false); + + // initial user is Bob + user = this._bob; + + // swaps users + getNewUser() { + return this.user = this.user === this._bob ? this._alice : this._bob; + } +} + +export class User { + constructor( + public name:string, + public isAuthorized:boolean = false) { } +} diff --git a/public/docs/_examples/dependency-injection/ts/example-config.json b/public/docs/_examples/dependency-injection/ts/example-config.json new file mode 100644 index 0000000000..e69de29bb2 diff --git a/public/docs/_examples/dependency-injection/ts/index.html b/public/docs/_examples/dependency-injection/ts/index.html new file mode 100644 index 0000000000..b05cd42835 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/index.html @@ -0,0 +1,34 @@ + + + + + + Dependency Injection + + + + + + + + + + + + + Loading my-app ... + Loading my-providers ... + + + diff --git a/public/docs/_examples/dependency-injection/ts/plnkr.json b/public/docs/_examples/dependency-injection/ts/plnkr.json new file mode 100644 index 0000000000..dff3dc4110 --- /dev/null +++ b/public/docs/_examples/dependency-injection/ts/plnkr.json @@ -0,0 +1,9 @@ +{ + "description": "Dependency Injection", + "files":[ + "!**/*.d.ts", + "!**/*.js", + "!**/*.[1,2].*" + ], + "tags": ["dependency", "di"] +} \ No newline at end of file diff --git a/public/docs/_examples/homepage-hello-world/ts/app/hello_world.js b/public/docs/_examples/homepage-hello-world/ts/app/hello_world.js new file mode 100644 index 0000000000..527306d4be --- /dev/null +++ b/public/docs/_examples/homepage-hello-world/ts/app/hello_world.js @@ -0,0 +1,39 @@ +System.register(['angular2/core'], function(exports_1) { + var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; + }; + var __metadata = (this && this.__metadata) || function (k, v) { + if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); + }; + var core_1; + var HelloWorld; + return { + setters:[ + function (core_1_1) { + core_1 = core_1_1; + }], + execute: function() { + HelloWorld = (function () { + function HelloWorld() { + // Declaring the variable for binding with initial value + this.yourName = ''; + } + HelloWorld = __decorate([ + core_1.Component({ + // Declare the tag name in index.html to where the component attaches + selector: 'hello-world', + // Location of the template for this component + templateUrl: 'app/hello_world.html' + }), + __metadata('design:paramtypes', []) + ], HelloWorld); + return HelloWorld; + })(); + exports_1("HelloWorld", HelloWorld); + } + } +}); +//# sourceMappingURL=hello_world.js.map \ No newline at end of file diff --git a/public/docs/_examples/homepage-hello-world/ts/app/main.js b/public/docs/_examples/homepage-hello-world/ts/app/main.js new file mode 100644 index 0000000000..5e99845524 --- /dev/null +++ b/public/docs/_examples/homepage-hello-world/ts/app/main.js @@ -0,0 +1,16 @@ +System.register(['angular2/platform/browser', './hello_world'], function(exports_1) { + var browser_1, hello_world_1; + return { + setters:[ + function (browser_1_1) { + browser_1 = browser_1_1; + }, + function (hello_world_1_1) { + hello_world_1 = hello_world_1_1; + }], + execute: function() { + browser_1.bootstrap(hello_world_1.HelloWorld); + } + } +}); +//# sourceMappingURL=main.js.map \ No newline at end of file diff --git a/public/docs/_examples/homepage-tabs/ts/app/di_demo.js b/public/docs/_examples/homepage-tabs/ts/app/di_demo.js new file mode 100644 index 0000000000..66ac5d61df --- /dev/null +++ b/public/docs/_examples/homepage-tabs/ts/app/di_demo.js @@ -0,0 +1,56 @@ +System.register(['angular2/core', './ui_tabs'], function(exports_1) { + var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; + }; + var __metadata = (this && this.__metadata) || function (k, v) { + if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); + }; + var core_1, ui_tabs_1; + var Detail, DiDemo; + return { + setters:[ + function (core_1_1) { + core_1 = core_1_1; + }, + function (ui_tabs_1_1) { + ui_tabs_1 = ui_tabs_1_1; + }], + execute: function() { + Detail = (function () { + function Detail() { + } + return Detail; + })(); + DiDemo = (function () { + function DiDemo() { + this.details = []; + this.id = 0; + } + DiDemo.prototype.addDetail = function () { + this.id++; + this.details.push({ + title: "Detail " + this.id, + text: "Some detail text for " + this.id + "..." + }); + }; + DiDemo.prototype.removeDetail = function (detail) { + this.details = this.details.filter(function (d) { return d !== detail; }); + }; + DiDemo = __decorate([ + core_1.Component({ + selector: 'di-demo', + template: "\n

Tabs Demo

\n \n \n \n \n \n
\n \n ", + directives: [ui_tabs_1.UiTabs, ui_tabs_1.UiPane] + }), + __metadata('design:paramtypes', []) + ], DiDemo); + return DiDemo; + })(); + exports_1("DiDemo", DiDemo); + } + } +}); +//# sourceMappingURL=di_demo.js.map \ No newline at end of file diff --git a/public/docs/_examples/homepage-tabs/ts/app/main.js b/public/docs/_examples/homepage-tabs/ts/app/main.js new file mode 100644 index 0000000000..164c7b05fb --- /dev/null +++ b/public/docs/_examples/homepage-tabs/ts/app/main.js @@ -0,0 +1,16 @@ +System.register(['angular2/platform/browser', './di_demo'], function(exports_1) { + var browser_1, di_demo_1; + return { + setters:[ + function (browser_1_1) { + browser_1 = browser_1_1; + }, + function (di_demo_1_1) { + di_demo_1 = di_demo_1_1; + }], + execute: function() { + browser_1.bootstrap(di_demo_1.DiDemo); + } + } +}); +//# sourceMappingURL=main.js.map \ No newline at end of file diff --git a/public/docs/_examples/homepage-tabs/ts/app/ui_tabs.js b/public/docs/_examples/homepage-tabs/ts/app/ui_tabs.js new file mode 100644 index 0000000000..017d5ff824 --- /dev/null +++ b/public/docs/_examples/homepage-tabs/ts/app/ui_tabs.js @@ -0,0 +1,85 @@ +System.register(['angular2/core'], function(exports_1) { + var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; + }; + var __metadata = (this && this.__metadata) || function (k, v) { + if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); + }; + var core_1; + var UiPane, UiTabs; + return { + setters:[ + function (core_1_1) { + core_1 = core_1_1; + }], + execute: function() { + UiPane = (function () { + function UiPane(viewContainer, templateRef) { + this.viewContainer = viewContainer; + this.templateRef = templateRef; + this._active = false; + } + Object.defineProperty(UiPane.prototype, "active", { + get: function () { + return this._active; + }, + set: function (active) { + if (active == this._active) + return; + this._active = active; + if (active) { + this.viewContainer.createEmbeddedView(this.templateRef); + } + else { + this.viewContainer.remove(0); + } + }, + enumerable: true, + configurable: true + }); + __decorate([ + core_1.Input(), + __metadata('design:type', String) + ], UiPane.prototype, "title", void 0); + __decorate([ + core_1.Input(), + __metadata('design:type', Boolean), + __metadata('design:paramtypes', [Boolean]) + ], UiPane.prototype, "active", null); + UiPane = __decorate([ + core_1.Directive({ + selector: '[ui-pane]' + }), + __metadata('design:paramtypes', [core_1.ViewContainerRef, core_1.TemplateRef]) + ], UiPane); + return UiPane; + })(); + exports_1("UiPane", UiPane); + UiTabs = (function () { + function UiTabs() { + } + UiTabs.prototype.select = function (pane) { + this.panes.toArray().forEach(function (p) { return p.active = p == pane; }); + }; + __decorate([ + core_1.ContentChildren(UiPane), + __metadata('design:type', core_1.QueryList) + ], UiTabs.prototype, "panes", void 0); + UiTabs = __decorate([ + core_1.Component({ + selector: 'ui-tabs', + template: "\n \n \n ", + styles: ['a { cursor: pointer; cursor: hand; }'] + }), + __metadata('design:paramtypes', []) + ], UiTabs); + return UiTabs; + })(); + exports_1("UiTabs", UiTabs); + } + } +}); +//# sourceMappingURL=ui_tabs.js.map \ No newline at end of file diff --git a/public/docs/_examples/homepage-todo/ts/app/main.js b/public/docs/_examples/homepage-todo/ts/app/main.js new file mode 100644 index 0000000000..ad9fb5517e --- /dev/null +++ b/public/docs/_examples/homepage-todo/ts/app/main.js @@ -0,0 +1,16 @@ +System.register(['angular2/platform/browser', './todo_app'], function(exports_1) { + var browser_1, todo_app_1; + return { + setters:[ + function (browser_1_1) { + browser_1 = browser_1_1; + }, + function (todo_app_1_1) { + todo_app_1 = todo_app_1_1; + }], + execute: function() { + browser_1.bootstrap(todo_app_1.TodoApp); + } + } +}); +//# sourceMappingURL=main.js.map \ No newline at end of file diff --git a/public/docs/_examples/homepage-todo/ts/app/todo.js b/public/docs/_examples/homepage-todo/ts/app/todo.js new file mode 100644 index 0000000000..787c7b7998 --- /dev/null +++ b/public/docs/_examples/homepage-todo/ts/app/todo.js @@ -0,0 +1,8 @@ +System.register([], function(exports_1) { + return { + setters:[], + execute: function() { + } + } +}); +//# sourceMappingURL=todo.js.map \ No newline at end of file diff --git a/public/docs/_examples/homepage-todo/ts/app/todo_app.js b/public/docs/_examples/homepage-todo/ts/app/todo_app.js new file mode 100644 index 0000000000..9fe7f097a1 --- /dev/null +++ b/public/docs/_examples/homepage-todo/ts/app/todo_app.js @@ -0,0 +1,66 @@ +System.register(['angular2/core', './todo_list', './todo_form'], function(exports_1) { + var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; + }; + var __metadata = (this && this.__metadata) || function (k, v) { + if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); + }; + var core_1, todo_list_1, todo_form_1; + var TodoApp; + return { + setters:[ + function (core_1_1) { + core_1 = core_1_1; + }, + function (todo_list_1_1) { + todo_list_1 = todo_list_1_1; + }, + function (todo_form_1_1) { + todo_form_1 = todo_form_1_1; + }], + execute: function() { + TodoApp = (function () { + function TodoApp() { + this.todos = [ + { text: 'learn angular', done: true }, + { text: 'build an angular app', done: false } + ]; + } + Object.defineProperty(TodoApp.prototype, "remaining", { + get: function () { + return this.todos.reduce(function (count, todo) { return count + +!todo.done; }, 0); + }, + enumerable: true, + configurable: true + }); + TodoApp.prototype.archive = function () { + var _this = this; + var oldTodos = this.todos; + this.todos = []; + oldTodos.forEach(function (todo) { + if (!todo.done) + _this.todos.push(todo); + }); + }; + TodoApp.prototype.addTask = function (task) { + this.todos.push(task); + }; + TodoApp = __decorate([ + core_1.Component({ + selector: 'todo-app', + template: "\n

Todo

\n {{remaining}} of {{todos.length}} remaining\n [ archive ]\n\n \n ", + styles: ['a { cursor: pointer; cursor: hand; }'], + directives: [todo_list_1.TodoList, todo_form_1.TodoForm] + }), + __metadata('design:paramtypes', []) + ], TodoApp); + return TodoApp; + })(); + exports_1("TodoApp", TodoApp); + } + } +}); +//# sourceMappingURL=todo_app.js.map \ No newline at end of file diff --git a/public/docs/_examples/homepage-todo/ts/app/todo_form.js b/public/docs/_examples/homepage-todo/ts/app/todo_form.js new file mode 100644 index 0000000000..2acea88537 --- /dev/null +++ b/public/docs/_examples/homepage-todo/ts/app/todo_form.js @@ -0,0 +1,47 @@ +System.register(['angular2/core'], function(exports_1) { + var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; + }; + var __metadata = (this && this.__metadata) || function (k, v) { + if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); + }; + var core_1; + var TodoForm; + return { + setters:[ + function (core_1_1) { + core_1 = core_1_1; + }], + execute: function() { + TodoForm = (function () { + function TodoForm() { + this.newTask = new core_1.EventEmitter(); + this.task = ''; + } + TodoForm.prototype.addTodo = function () { + if (this.task) { + this.newTask.next({ text: this.task, done: false }); + } + this.task = ''; + }; + __decorate([ + core_1.Output(), + __metadata('design:type', Object) + ], TodoForm.prototype, "newTask", void 0); + TodoForm = __decorate([ + core_1.Component({ + selector: 'todo-form', + template: "\n
\n \n \n
" + }), + __metadata('design:paramtypes', []) + ], TodoForm); + return TodoForm; + })(); + exports_1("TodoForm", TodoForm); + } + } +}); +//# sourceMappingURL=todo_form.js.map \ No newline at end of file diff --git a/public/docs/_examples/homepage-todo/ts/app/todo_list.js b/public/docs/_examples/homepage-todo/ts/app/todo_list.js new file mode 100644 index 0000000000..88f4c6b0c6 --- /dev/null +++ b/public/docs/_examples/homepage-todo/ts/app/todo_list.js @@ -0,0 +1,41 @@ +System.register(['angular2/core'], function(exports_1) { + var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; + }; + var __metadata = (this && this.__metadata) || function (k, v) { + if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); + }; + var core_1; + var TodoList; + return { + setters:[ + function (core_1_1) { + core_1 = core_1_1; + }], + execute: function() { + TodoList = (function () { + function TodoList() { + } + __decorate([ + core_1.Input(), + __metadata('design:type', Array) + ], TodoList.prototype, "todos", void 0); + TodoList = __decorate([ + core_1.Component({ + selector: 'todo-list', + styles: ["\n .done-true {\n text-decoration: line-through;\n color: grey;\n }" + ], + template: "\n " + }), + __metadata('design:paramtypes', []) + ], TodoList); + return TodoList; + })(); + exports_1("TodoList", TodoList); + } + } +}); +//# sourceMappingURL=todo_list.js.map \ No newline at end of file diff --git a/public/docs/ts/latest/guide/dependency-injection.jade b/public/docs/ts/latest/guide/dependency-injection.jade index b1a9370b5b..e180df003d 100644 --- a/public/docs/ts/latest/guide/dependency-injection.jade +++ b/public/docs/ts/latest/guide/dependency-injection.jade @@ -1,35 +1,27 @@ include ../../../../_includes/_util-fns :marked - Dependency Injection is an important application design pattern. - Angular has its own Dependency Injection framework and + **Dependency Injection** is an important application design pattern. + Angular has its own dependency injection framework and we really can't build an Angular application without it. + It's used so widely that almost everyone just calls it "DI". - In this chapter we'll learn what Dependency Injection is, why we want it, and how to use it. - + In this chapter we'll learn [what DI is, why](#why-di) we want it. + Then we'll learn [how to use it](#angular-di) in an Angular app. + + All code in this chapter is available as a + [live example](/resources/live-examples/dependency-injection/ts/plnkr.html) + on the web. + + .l-main-section :marked ## Why Dependency Injection? Let's start with the following code. - ``` - class Engine {} - - class Tires {} - - class Car { - private engine: Engine; - private tires: Tires; - - constructor() { - this.engine = new Engine(); - this.tires = new Tires(); - } - // Method using the engine and tires - drive() {} - } - ``` ++makeExample('dependency-injection/ts/app/car/car-no-di.ts', 'car', 'app/car/car.ts (no di)') +:marked Our `Car` creates everything it needs inside its constructor. What's the problem? @@ -73,21 +65,28 @@ include ../../../../_includes/_util-fns How can we make `Car` more robust, more flexible, and more testable? That's super easy. We probably already know what to do. We change our `Car` constructor to this: - - ``` - constructor(engine: Engine, tires: Tires) { - this.engine = engine; - this.tires = tires; - } - ``` + + + ++makeTabs( + 'dependency-injection/ts/app/car/car.ts, dependency-injection/ts/app/car/car-no-di.ts', + 'car-ctor, car-ctor', + 'app/car/car.ts DI (constructor), app/car/car.ts No DI (constructor)')(format=".") + +:marked See what happened? We moved the definition of the dependencies to the constructor. Our `Car` class no longer creates an engine or tires. It just consumes them. +.l-sub-section + :marked + We also leverage TypeScript's constructor syntax for declaring parameters and properties simultaneously. +:marked Now we create a car by passing the engine and tires to the constructor. - ``` - var car = new Car(new Engine(), new Tires()); - ``` + ++makeExample('dependency-injection/ts/app/car/car-creations.ts', 'car-ctor-instantiation', 'Simple Car')(format=".") + +:marked How cool is that? The definition of the engine and tire dependencies are decoupled from the `Car` class itself. We can pass in any kind of engine or tires we like, as long as they @@ -98,9 +97,10 @@ include ../../../../_includes/_util-fns :marked The consumer of `Car` has the problem. The consumer must update the car creation code to something like: - ``` - var car = new Car(new Engine(theNewParameter), new Tires()); - ``` + + +makeExample('dependency-injection/ts/app/car/car-creations.ts', 'car-ctor-instantiation-with-param', 'Super Car')(format=".") + + :marked The critical point is this: `Car` itself did not have to change. We'll take care of the consumer's problem soon enough. @@ -109,10 +109,10 @@ include ../../../../_includes/_util-fns of its dependencies. We can pass mocks to the constructor that do exactly what we want them to do during each test: - ``` - var car = new Car(new MockEngine(), new MockLowPressureTires()); - ``` ++makeExample('dependency-injection/ts/app/car/car-creations.ts', 'car-ctor-instantiation-with-mocks', 'Test Car')(format=".") + +:marked **We just learned what Dependency Injection is**. It's a coding pattern in which a class receives its dependencies from external @@ -125,13 +125,10 @@ include ../../../../_includes/_util-fns We need something that takes care of assembling these parts for us. We could write a giant class to do that: - ``` - class SuperFactory { - createEngine = () => new Engine(); - createTires = () => new Tires(); - createCar = () => new Car(this.createEngine(), this.createTires()); - } - ``` + ++makeExample('dependency-injection/ts/app/car/car-factory.ts', null, 'app/car/car-factory.ts') + +:marked It's not so bad now with only three creation methods. But maintaining it will be hairy as the application grows. This `SuperFactory` is going to become a huge spider web of @@ -145,23 +142,21 @@ include ../../../../_includes/_util-fns We register some classes with this `Injector` and it figures out how to create them. When we need a `Car`, we simply ask the `Injector` to get it for us and we're good to go. - ``` - function main() { - var injector = new Injector([Car, Engine, Tires, Logger]); - var car = injector.get(Car); - car.drive(); - } - ``` + ++makeExample('dependency-injection/ts/app/car/car-injector.ts','injector-call')(format=".") + +:marked Everyone wins. The `Car` knows nothing about creating an `Engine` or `Tires`. The consumer knows nothing about creating a `Car`. We don't have a gigantic factory class to maintain. Both `Car` and consumer simply ask for what they need and the `Injector` delivers. - This is what a **Dependency InjectionFramework** is all about. + This is what a **Dependency Injection Framework** is all about. Now that we know what Dependency Injection is and appreciate its benefits, let's see how it is implemented in Angular. + .l-main-section :marked ## Angular Dependency Injection @@ -174,383 +169,525 @@ include ../../../../_includes/_util-fns We'll begin with a simplified version of the `HeroesComponent` that we built in the [The Tour of Heroes](../tutorial/). - ``` - import {Component} from 'angular2/angular2'; - import {Hero} from './hero'; - import {HEROES} from './mock-heroes'; - @Component({ - selector: 'my-heroes' - templateUrl: 'app/heroes.component.html' - }) - export class HeroesComponent { ++makeTabs( + `dependency-injection/ts/app/heroes/heroes.component.1.ts, + dependency-injection/ts/app/heroes/hero-list.component.1.ts, + dependency-injection/ts/app/heroes/hero.ts, + dependency-injection/ts/app/heroes/mock-heroes.ts`, + 'v1,,,', + `app/heroes/heroes.component.ts, + app/heroes/hero-list.component.ts, + app/heroes/hero.ts, + app/heroes/mock-heroes.ts`) - heroes: Hero[] = HEROES; - - } - ``` - It assigns a list of mocked heroes to its `heroes` property for binding within the template. - Pretty straight forward. - - Those heroes are currently a fixed, in-memory collection, defined in another file and imported by the component. - That works in the early stages of development but it's far from ideal. +:marked + The `HeroesComponent` is the root component of the *Heroes* feature area. + It governs all the child components of this area. + In our stripped down version there is only one child, `HeroListComponent`, + dedicated to displaying a list of heroes. +.l-sub-section + :marked + Do we really need so many files? Of course not! + We're going *beyond* the strictly necessary + in order to illustrate patterns that will serve us well in real applications. + With each file we can make one or two points rather than crowd them all into one file. +:marked + Right now it gets heroes from `HEREOES`, an in-memory collection, + defined in another file and imported by this component. + That may suffice in the early stages of development but it's far from ideal. As soon as we try to test this component or want to get our heroes data from a remote server, - we'll have to change this component's implementation of `heroes` and + we'll have to change the implementation of `heroes` and fix every other use of the `HEROES` mock data. - Let's make a service that hides how we get Hero data. + Let's make a service that hides how we get hero data. .l-sub-section :marked Write this service in its own file. See [this note](#forward-ref) to understand why. + ++makeExample('dependency-injection/ts/app/heroes/hero.service.1.ts',null, 'app/heroes/hero.service.ts' ) :marked - ``` - import {Hero} from './hero'; - import {HEROES} from './mock-heroes'; - - class HeroService { - - heroes: Hero[]; - - constructor() { - this.heroes = HEROES; - } - - getHeroes() { - return this.heroes; - } - } - ``` Our `HeroService` exposes a `getHeroes()` method that returns the same mock data as before but none of its consumers need to know that. - +.l-sub-section + :marked + We aren't even pretending this is a real service. + If we were actually getting data from a remote server, the API would have to be asynchronous, + perhaps returning + [ES2015 promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) + We'd also have to rewrite the way components consume our service. + This is important but not important to our current story. +:marked A service is nothing more than a class in Angular 2. - It remains nothing more than a class until we register it with - the Angular injector. + It remains nothing more than a class until we register it with an Angular injector. ### Configuring the Injector - We don't have to create the injector. - + + We don't have to create an Angular injector. Angular creates an application-wide injector for us during the bootstrap process. - ``` - bootstrap(HeroesComponent); - ``` - Let’s configure the injector at the same time that we bootstrap by adding - our `HeroService` to an array in the second argument. - We'll explain that array when we talk about [providers](#providers) later in this chapter. - ``` - bootstrap(AppComponent, [HeroService]); - ``` - That’s it! The injector now knows about the `HeroService` which is available for injection across our entire application. ++makeExample('dependency-injection/ts/app/main.ts', 'bootstrap', 'app/main.ts (bootstrap)')(format='.') +:marked + We do have to configure the injector by registering the **providers** + that create the services we need in our application. + We'll explain what [providers](#providers) are later in this chapter. + Before we do, let's see an example of provider registration during bootstrapping: + ++makeExample('dependency-injection/ts/app/main.1.ts', 'bootstrap')(format='.') +:marked + The injector now knows about our `HeroService`. + An instance of our `HeroService` will be available for injection across our entire application. - ### Preparing the `HeroesComponent` for injection + Of course we can't help wondering about that comment telling us not to do it this way. + It *will* work. It's just not a best practice. + The bootstrap provider option is intended for configuring and overriding Angular's own + pre-registered services. + + The preferred approach is to register application providers in application components. + Because the `HeroService` will be used within the *Heroes* feature area — + and nowhere else — the ideal place to register it is in the top-level `HeroesComponent`. + + ### Registering providers in a component + Here's a revised `HeroesComponent` that registers the `HeroService`. + ++makeExample('dependency-injection/ts/app/heroes/heroes.component.1.ts',null,'app/heroes/heroes.component.ts') +:marked + Look closely at the bottom of the `@Component` metadata: + ++makeExample('dependency-injection/ts/app/heroes/heroes.component.1.ts','providers')(format='.') +:marked + An instance of the `HeroService` is now available for injection in this `HeroesComponent` + and all of its child components. + + The `HeroesComponent` itself doesn't happen to need the `HeroService`. + But its child `HeroListComponent` does so we head there next. + + ### Preparing the *HeroListComponent* for injection - The `HeroesComponent` should get its heroes from this service. + The `HeroListComponent` should get heroes from the injected `HeroService`. Per the dependency injection pattern, the component must "ask for" the service in its constructor [as we explained - earlier](#ctor-injection)". + earlier](#ctor-injection). + It's a small change as we see in this comparison: ++makeTabs( + `dependency-injection/ts/app/heroes/hero-list.component.2.ts, + dependency-injection/ts/app/heroes/hero-list.component.1.ts`, + null, + `app/heroes/hero-list.component (DI), + app/heroes/hero-list.component (no DI)`) +:marked - ``` - constructor(heroService: HeroService) { - this.heroes = heroService.getHeroes(); - } - ``` - .l-sub-section + :marked + ### Focus on the constructor + +makeExample('dependency-injection/ts/app/heroes/hero-list.component.2.ts', 'ctor')(format=".") :marked Adding a parameter to the constructor isn't all that's happening here. - We are writing the app in TypeScript and have followed the parameter name with a type notation, `:HeroService`. + We're writing in TypeScript and have followed the parameter name with a type annotation, `:HeroService`. The class is also decorated with the `@Component` decorator (scroll up to confirm that fact). - When the TypeScript compiler evaluates this class, it sees the decorator and adds class metadata + When the TypeScript compiler evaluates this class, it sees the `@Component` decorator and adds class metadata into the generated JavaScript code. Within that metadata lurks the information that associates the `heroService` parameter with the `HeroService` class. - That's how the Angular injector will know to inject an instance of the `HeroService` when it - creates a new `HeroesComponent`. + That's how the Angular injector knows to inject an instance of the `HeroService` when it + creates a new `HeroListComponent`. + :marked - ### Creating the `HeroesComponent` with the injector (implicitly) + ### Creating the injector (implicitly) When we introduced the idea of an injector above, we showed how to create - a new `Car` with that injector. - ``` - var car = injector.get(Car); - ``` - Search the entire Tour of Heroes source. We won't find a single line like - ``` - var hc = injector.get(HeroesComponent); - ``` - We *could* write code like that if we wanted to. We just don't have to. - Angular does that for us when it renders a `HeroesComponent` - whether we ask for it in an HTML template ... - ``` - - ``` - ... or navigate to a `HeroesComponent` view with the [router](./router.html). + an injector and use it to create a new `Car`. + ++makeExample('dependency-injection/ts/app/car/car-injector.ts','injector-create-and-call')(format=".") + +:marked + We won't find code like that in the Tour of Heroes or any of our other samples. + We *could* write code [like that](#explicit-injector) if we *had* to which we rarely do. + Angular takes care of creating and calling injectors + when it creates components for us whether through HTML markup, as in ``, + or after navigating to a component with the [router](./router.html). + Let Angular do its job and we'll enjoy the benefits of automated dependency injection. ### Singleton services - We might wonder what happens when we inject the `HeroService` into other components. - Do we get the same instance every time? + Dependencies are singletons within the scope of an injector. + In our example, there is a single `HeroService` instance shared among the + `HeroesComponent` and its `HeroListComponent` children. - Yes we do. Dependencies are singletons. - We’ll discuss that later in our chapter about - [Hierarchical Injectors](./hierarchical-dependency-injection.html). + However, Angular DI is an hierarchical injection + system which means nested injectors can create their own service instances. + Learn more about that in the [Hierarchical Injection](./hierarchical-dependency-injection.html) chapter. ### Testing the component - We emphasized earlier that designing a class for dependency injection makes it easier to test. + We emphasized earlier that designing a class for dependency injection makes the class easier to test. + Listing dependencies as constructor parameters may be all we need to test application parts effectively. + + For example, we can create a new `HeroListComponent` with a mock service that we can manipulate + under test: - Mission accomplished! We don't even need the Angular Dependency Injection system to test the `HeroesComponent`. - We simply create a new `HeroesComponent` with a mock service and poke at it: - ``` - it("should have heroes when created", () => { - let hc = new HeroesComponent(mockService); - expect(hc.heroes.length).toEqual(mockService.getHeroes().length); - }) - ``` ++makeExample('dependency-injection/ts/app/test.component.ts', 'spec')(format='.') +.l-sub-section + :marked + Learn more in the [Testing](../testing/index.html) chapter. + +:marked ### When the service needs a service Our `HeroService` is very simple. It doesn't have any dependencies of its own. What if it had a dependency? What if it reported its activities through a logging service? - We'd apply the same "constructor injection" pattern. + We'd apply the same *constructor injection* pattern, + adding a constructor that takes a `logger` parameter. - Here's a rewrite of `HeroService` with a new constructor that takes a `logger` parameter. - ``` - import {Injectable} from 'angular2/angular2'; - import {Hero} from './hero'; - import {HEROES} from './mock-heroes'; - import {Logger} from './logger'; + Here is the revision compared to the original. + ++makeTabs( + `dependency-injection/ts/app/heroes/hero.service.2.ts, + dependency-injection/ts/app/heroes/hero.service.1.ts`, + null, + `app/heroes/hero.service (v.2), + app/heroes/hero.service (v.1)`) - @Injectable() - class HeroService { - - heroes: Hero[]; - - constructor(private logger: Logger) { - this.heroes = HEROES; - } - - getHeroes() { - this.logger.log('Getting heroes ...') - return this.heroes; - } - } - ``` - The constructor now asks for an injected instance of a `Logger` and stores it in a private property called `logger`. +:marked + The constructor now asks for an injected instance of a `Logger` and stores it in a private property called `_logger`. We call that property within our `getHeroes()` method when anyone asks for heroes. - **The `@Injectable()` decoration catches our eye!** + + ### Why *@Injectable*? + Notice the `@Injectable()` decoration above the service class. + We haven't seen `@Injectable()` before. + As it happens, we could have added it to our first version`HeroService`. + We didn't bother because we didn't need it then. + + We need it now... now that our service has an injected dependency. + We need it because Angular requires constructor parameter metadata in order to inject a `Logger`. + As [we mentioned earlier](#di-metadata), TypeScript *only generates metadata for classes that have a decorator*. . + +.callout.is-helpful + header Always add @Injectable() + :marked + We recommend adding `@Injectable()` to every service class, even those that don't have dependencies + and, therefore, do not technically require it. Two reasons: + ol(style="font-size:inherit") + li Future proofing: no need to remember @Injectable when we add a dependency later. + li Consistency: all services follow the same rules and we don't have to wonder why a decorator is missing. +.l-sub-section + :marked + The `HeroesComponent` has an injected dependency too. Why don't we add `@Injectable()` to the `HeroesComponent`? + + We *can* add it if we really want to. It isn't necessary because the `HeroesComponent` is already decorated with `@Component`. + TypeScript generates metadata for *any* class with a decorator and *any* decorator will do. .alert.is-critical :marked - **Always include the parentheses!** Always call `@Injectable()`. It's easy to forget the parentheses. - Our application will fail mysteriously if we do. It bears repeating: **always include the parentheses.** -:marked - We haven't seen `@Injectable()` before. - As it happens, we could have added it to `HeroService`. We didn't bother because we didn't need it then. - - We need it now ... now that our service has an injected dependency. - We need it because Angular requires constructor parameter metadata in order to inject a `Logger`. - As [we mentioned earlier](#di-metadata), TypeScript *only generates metadata for classes that have a decorator*. . - - The `HeroesComponent` has an injected dependency too. Why don't we add `@Injectable()` to the `HeroesComponent`? - We *can* add it if we really want to. It isn't necessary because the `HeroesComponent` is already decorated with `@Component`. - TypeScript generates metadata for *any* class with a decorator and *any* decorator will do. + **Always include the parentheses!** Always call `@Injectable()`. + Our application will fail mysteriously if we forget the parentheses. .l-main-section :marked - + ## Create and register the *Logger* service + We're injecting a Logger into our `HeroService`. + We have two remaining steps: + 1. Create the Logger service + 1. Register it with the application + + The logger service implementation is no big deal. + ++makeExample( + 'dependency-injection/ts/app/logger.service.ts',null, 'app/logger.service') +:marked + We're likely to need the same logger service everywhere in our application + so we put it at the root level of the application in the `app/` folder and + we register it in the `providers` array of the metadata for our application root component, `AppComponent`. ++makeExample('dependency-injection/ts/app/providers.component.ts','providers-1', 'app/app.component.ts (providers)') +:marked + If we forget to register it, Angular will throw an exception when it first needs the logger: +code-example(format). + EXCEPTION: No provider for Logger! (HeroListComponent -> HeroService -> Logger) +:marked + That's telling us that the dependency injector couldn't find the *provider* for the logger + when it first tried to call the logger — inside the `HeroService` when the `HeroListComponent` + was calling for heroes. + The *provider* is the subject of our next section. + + But wait! What if the logger is optional? + + ### Optional dependencies + + Our `HeroService` currently requires a `Logger`. What if we could get by without a logger? + We'd use it if we had it, ignore it if we didn't. + + First we import the `@Optional` decorator. ++makeExample('dependency-injection/ts/app/providers.component.ts','import-optional')(format='.') +:marked + Then rewrite the constructor with that decorator to make the logger optional. ++makeExample('dependency-injection/ts/app/providers.component.ts','provider-10-ctor')(format='.') +:marked + Be prepared for a null logger. If we don't register one somewhere up the line, + the injector will inject `null`. We have a method that logs. What can we do? + + We could substitute a *do-nothing* logger stub so that our methods continue to work: ++makeExample('dependency-injection/ts/app/providers.component.ts','provider-10-logger')(format='.') +:marked + Obviously we'd take a more sophisticated approach if the logger were optional + elsewhere as well. + + But enough about optional loggers. In our sample application, the `Logger` is required. + We must register a `Logger` with the application injector using *providers* + as we learn in the next section. + + +.l-main-section +:marked ## Injector Providers + + A provider *provides* the concrete, runtime version of a dependency value. + The injector relies on **providers** to create instances of the services + that it injects into components and other services. - Remember when we added the `HeroService` to an array in the [bootstrap](#bootstrap) process? - ``` - bootstrap(AppComponent, [HeroService]); - ``` - That list of classes is actually a list of **providers**. - - "Providers" create the instances of the things that we ask the injector to inject. - There are many ways to "provide" a thing that has the necessary shape and behavior to serve as a `HeroService`. - A class is a natural provider - it's meant to be created. But it's not the only way - to produce something injectable. We could hand the injector an object to return. We could give it a factory function to call. + We must register *providers* with the injector or it won't know what to do. + + Earlier we registered the `Logger` service in the `providers` array of the metadata for the `AppComponent` like this: ++makeExample('dependency-injection/ts/app/providers.component.ts','providers-logger') +:marked + The `providers` array appears to hold service classes (one service class in this example). + In reality it holds instances of the [Provider](../api/core/Provider-class.html) class. + + In our example, when the `HeroService` constructor specifies `logger:Logger`, it's expecting + something that has the shape and behavior of the `Logger` class. + + There are many ways to *provide* something that has the shape and behavior of a `Logger`. + The `Logger` class itself is an obvious and natural provider - it has the right shape and it's designed to be created. + But it's not the only way. + + We can configure the injector with alternative providers that can deliver an object that behaves like a `Logger`. + We could provide a substitute class. We could provide a logger-like object. + We could give it a provider that calls a logger factory function. Any of these approaches might be a good choice under the right circumstances. - What matters is that the injector knows what to do when something asks for a `HeroService`. + What matters is that the injector has a provider to go to when it needs a `Logger`. - ### The Provider Class + + ### The *provide* function - When we wrote ... - ``` - [HeroService]; - ``` - we used a short-hand expression for provider registration. - Angular expanded that short-hand into a call to the Angular `provide` method - ``` - [provide(HeroService, {useClass:HeroService})]; - ``` - and the `provide` method in turn creates a new instance of the Angular - [Provider class](/docs/ts/latest/api/core/Provider-class.html): - ``` - [new Provider(HeroService, {useClass:HeroService})] - ``` - This provider instance associates a `HeroService` *token* - with code that can create an *instance* of a `HeroService`. + We wrote the `providers` array like this: ++makeExample('dependency-injection/ts/app/providers.component.ts','providers-1') +:marked + This is actually a short-hand expression for a provider registration that creates a new instance of the + [Provider](/docs/ts/latest/api/core/Provider-class.html) class. ++makeExample('dependency-injection/ts/app/providers.component.ts','providers-2') +:marked + The [provide](../api/core/provide-function.html) function is the more common and friendlier way to create a `Provider`: ++makeExample('dependency-injection/ts/app/providers.component.ts','providers-3') +:marked + In both approaches — `Provider` class and `provide` function — + we supply two arguments. - The first parameter is the [token](#token) that serves as the key for both locating a dependency value + The first is the [token](#token) that serves as the key for both locating a dependency value and registering the provider. - The second parameter is a provider definition object - which we think of as a "recipe" for creating the dependency value. + The second is a provider definition object + which we think of as a *recipe* for creating the dependency value. There are many ways to create dependency values ... and many ways to write a recipe. + ### Alternative Class Providers - Occasionally we'll ask a different class to provide the service. - - We do that regularly when testing a component that we're creating with dependency injection. + Occasionally we'll ask a different class to provide the service. In this example, we tell the injector - to return a `MockHeroService` when something asks for the `HeroService`. - ``` - beforeEachProviders(() => [ - provide(HeroService, {useClass: MockHeroService}); - ]); - ``` + to return a `BetterLogger` when something asks for the `Logger`. + ++makeExample('dependency-injection/ts/app/providers.component.ts','providers-4') +:marked + ### Class provider with dependencies + Maybe an `EvenBetterLogger` could display the user name in the log message. + This logger gets the user from the injected `UserService` + which happens also to be injected at the application level. ++makeExample('dependency-injection/ts/app/providers.component.ts','EvenBetterLogger') +:marked + Configure it like we did `BetterLogger`. ++makeExample('dependency-injection/ts/app/providers.component.ts','providers-5')(format=".") +:marked + ### Aliased Class Providers + + Suppose there is an old component that depends upon an `OldLogger` class. + `OldLogger` has the same interface as the `NewLogger` but, for some reason, + we can't update the old component to use it. + + When the *old* component logs a message with `OldLogger`, + we want the one singleton instance of `NewLogger` to handle it instead. + + The dependency injector should inject that singleton instance + when a component asks for either the new or the the old logger. + The `OldLogger` should be an alias for `NewLogger`. + + We certainly do not want two different `NewLogger` instances in our app. + Unfortunately, that's what we get if we try to alias `OldLogger` to `NewLogger` with `useClass`. + ++makeExample('dependency-injection/ts/app/providers.component.ts','providers-6a')(format=".") +:marked + Alias with the `useExisting` option instead. ++makeExample('dependency-injection/ts/app/providers.component.ts','providers-6b')(format=".") + + +:marked ### Value Providers Sometimes it's easier to provide a ready-made object rather than ask the injector to create it from a class. ++makeExample('dependency-injection/ts/app/providers.component.ts','silent-logger')(format=".") +:marked + Then we register a provider with this object playing the logger role via the `useValue` option. ++makeExample('dependency-injection/ts/app/providers.component.ts','providers-7')(format=".") - We do that a lot when we write tests. We might write the following test setup - for tests that explore how the `HeroComponent` behaves when the `HeroService` - returns an empty hero list. - ``` - beforeEachProviders(() => { - - let emptyHeroService = { getHeroes: () => [] }; - - return [ provide(HeroService, {useValue: emptyHeroService}) ]; - }); - ``` - Notice we defined the recipe with `useValue` instead of `useClass`. - + +:marked ### Factory Providers - Sometimes the best choice for a provider is neither a class nor a value. - - Suppose our HeroService has some cool new feature that we're only offering to "special" users. - The HeroService shouldn't know about users and - we won't know if the current user is special until runtime anyway. - We decide to extend our `HeroService` constructor to accept a `useCoolFeature` flag - that toggles the feature on or off. - We rewrite the `HeroService` again as follows. - ``` - @Injectable() - class HeroService { - - heroes: Hero[]; - - constructor(private logger: Logger, private useCoolFeature: boolean) { - this.heroes = HEROES; - } - - getHeroes() { - let msg = this.useCoolFeature ? 'the cool new way' : 'the old way'; - this.logger.log('Getting heroes ...' + msg) - return this.heroes; - } - } - ``` - The feature flag is a simple boolean value. We'd like to inject the flag but it seems silly to write an entire class for a - simple flag. - - We can replace the `HeroService` provider with a factory function that creates a properly configured `HeroService` for the current user. - We'll' build up to that result, beginning with our definition of the factory function: - ``` - let heroServiceFactory = (logger: Logger, userService: UserService) => { - return new HeroService(logger, userService.user.isSpecial); - } - ``` + Sometimes we need to create the dependent value dynamically, + based on information we won't have until the last possible moment. + Maybe the information can change. + + Suppose also that the injectable service has no independent access to the source of this information. + + This situation calls for a **factory provider** as we illustrate next. + + Our HeroService should hide *secret* heroes from normal users. + Only authorized users should see them. + + Like the `EvenBetterLogger`, the `HeroService` needs a fact about the user. + It needs to know if the user is authorized to see secret heroes. + That authorization can change during the course of a single application session + as when we log in a different user. + + Unlike `EvenBetterLogger`, we can't inject the `UserService` into the `HeroService`. .l-sub-section :marked - The factory takes two parameters: the logger service and a user service. - The logger we pass straight to the constructor as we did before. - - We'll know to use the cool new feature if the `userService.user.isSpecial` flag is true, - a fact we can't know until runtime. + Why? We don't know either. Stuff like this happens. :marked - We use dependency injection everywhere so of course the factory function depends on - two injected services: `Logger` and `UserService`. - We declare those requirements in our provider definition object: - ``` - let heroServiceDefinition = { - useFactory: heroServiceFactory, - deps: [Logger, UserService] - }; - ``` + Instead the `HeroService` constructor takes a boolean flag to control display of secret heroes. ++makeExample('dependency-injection/ts/app/heroes/hero.service.ts','internals', 'app/heroes/hero.service.ts (internals)')(format='.') +:marked + We can inject the `Logger` but we can't inject the boolean `isAuthorized`. + We'll have to take over the creation of new instances of this `HeroService` with a factory provider. + + A factory provider needs a factory function: ++makeExample('dependency-injection/ts/app/heroes/hero.service.provider.ts','factory', 'app/heroes/hero.service.provider.ts (factory)')(format='.') +:marked + Although our `HeroService` knows nothing about the `UserService`, our factory function does. + + We inject both the `Logger` and the `UserService` into the factory provider: ++makeExample('dependency-injection/ts/app/heroes/hero.service.provider.ts','provider', 'app/heroes/hero.service.provider.ts (provider)')(format='.') +:marked + .l-sub-section :marked - The `useFactory` field tells Angular that the provider is a factory function and that its implementation is the `heroServiceFactory`. + The `useFactory` field tells Angular that the provider is a factory function + whose implementation is the `heroServiceFactory`. The `deps` property is an array of [provider tokens](#token). The `Logger` and `UserService` classes serve as tokens for their own class providers. :marked - Finally, we create the provider and adjust the bootstrapping to include that provider - among its provider registrations. - ``` - let heroServiceProvider = provide(HeroService, heroServiceDefinition); + Notice that we've captured the factory provider in an exported variable, `heroServiceProvider`. + This extra step makes it easier for us to register our `HeroService` whereever we need it. + + In our sample, we need it only in the `HeroesComponent` + where it replaces the previous `HeroService` registration in the metadata `providers` array: ++makeTabs( + `dependency-injection/ts/app/heroes/heroes.component.ts, + dependency-injection/ts/app/heroes/heroes.component.1.ts`, + null, + `app/heroes/heroes.component (v.3), + app/heroes/heroes.component (v.2)`) + + +.l-main-section +:marked + ## Dependency Injection Tokens - bootstrap(AppComponent, [heroServiceProvider, Logger, UserService]); - ``` - - ### String tokens - - Sometimes we have an object dependency rather than a class dependency. + When we register a provider with an injector we associate that provider with a dependency injection token. + The injector maintains an internal *token/provider* map that it references when + asked for a dependency + + In all previous examples, the dependency value has been a class *instance* and + the class *type* served as its own lookup token. + Here we get a `HeroService` directly from the injector by supplying the `HeroService` type as the token. ++makeExample('dependency-injection/ts/app/injector.component.ts','get-hero-service')(format='.') +:marked + We have similar good fortune (in typescript) when we write a constructor that requires an injected class-based dependency. + We define a constructor parameter with the `HeroService` class type and Angular knows to inject the + service associated with that `HeroService` class token: ++makeExample('dependency-injection/ts/app/providers.component.ts','provider-8-ctor')(format=".") +:marked + This is all especially convenient when we consider that most dependency values are provided by classes. + + ### Non-class Dependencies + + What if the dependency value isn't a class? + Sometimes the thing we want to inject is a string, a function, or an object. Applications often define configuration objects with lots of small facts like the title of the application or the address of a web api endpoint. These configuration objects aren't always instances of a class. They're just objects ... like this one: - ``` - let config = { - apiEndpoint: 'api.heroes.com', - title: 'The Hero Employment Agency' - }; - ``` + ++makeExample('dependency-injection/ts/app/app.config.ts','config','app/app-config.ts')(format='.') +:marked We'd like to make this `config` object available for injection. - We know we can register an object with a "Value Provider". But what do we use for the token? + We know we can register an object with a [Value Provider](#value-provider). + But what do we use for the token? + We don't have a class to serve as a token. There is no `Config` class. + +// begin Typescript only + +:marked + The `CONFIG` constant has an interface, `Config`. Unfortunately, we + **cannot use an interface as a token** ++makeExample('dependency-injection/ts/app/providers.component.ts','providers-9a-interface')(format=".") ++makeExample('dependency-injection/ts/app/providers.component.ts','provider-9a-ctor-interface')(format=".") +:marked + It's not Angular's fault. An interface is a TypeScript design-time artifact. + It disappears from the generated JavaScript so there is no interface type information for Angular to find at runtime. +// end Typescript only + +:marked + ### String tokens + Fortunately, we can register any dependency provider with a string token. ++makeExample('dependency-injection/ts/app/providers.component.ts','providers-9a')(format=".") +:marked + Now we inject the configuration object into any constructor that needs it with + the help of an `@Inject` decorator to tell Angular how to find the configuration dependency value. ++makeExample('dependency-injection/ts/app/providers.component.ts','provider-9a-ctor')(format=".") +:marked + +// begin Typescript only +.l-sub-section + :marked + Although it plays no role in dependency injection, + the `Config` interface supports strongly-typing of the configuration object within the class. +:marked +// end typescript only - - Until now, we've always asked the class to play the token role - whether we wrote a provider with a class, value, or factory recipe. - This time we don't have a class to serve as a token. There is no `Config` class. + +:marked + ### OpaqueToken + Alternatively, we could define and use an [OpaqueToken](../api/core/OpaqueToken-class.html) + rather than rely on magic strings that may collide with other developers' string choices. + + The definition looks like this: ++makeExample('dependency-injection/ts/app/app.config.ts','token')(format='.') +:marked + Substitute `APP_CONFIG` for the magic string in provider registration and constructor: ++makeExample('dependency-injection/ts/app/providers.component.ts','providers-9b')(format=".") ++makeExample('dependency-injection/ts/app/providers.component.ts','provider-9b-ctor')(format=".") +:marked + Here's how we provide and inject the configuration object in our top-level `AppComponent`. ++makeExample('dependency-injection/ts/app/app.component.ts','providers', 'app/app.component.ts (providers)')(format=".") ++makeExample('dependency-injection/ts/app/app.component.ts','ctor', 'app/app.component.ts (constructor)')(format=".") - Fortunately, the token can be a string, a class type, or an - [OpaqueToken](/docs/ts/latest/api/core/OpaqueToken-class.html). - Internally, the `Provider` turns the string and class parameter into an `OpaqueToken`; - the injector locates dependency values and providers by this token. - - We'll register our configuration object with a string-based token! - ``` - bootstrap(AppComponent, [ - // other providers // - provide('App.config', {useValue:config}) - ]); - ``` - - Let's apply what we've learned and update the `HeroesComponent` constructor so it can display the configured title. - Right now the constructor signature is - ``` - constructor(heroService: HeroService) - ``` - We might think we can add the `config` dependency by writing: - ``` - // FAIL! - constructor(heroService: HeroService, config: config) - ``` - That's not going to work. There is no type called `config` and we didn't register the `config` object under that name anyway. - We'll need a little help from another Angular decorator called `@Inject`. - ``` - import {Inject} from 'angular2/angular2' - - constructor(heroService: HeroService, @Inject('app.config') config) - - ``` +.l-sub-section + :marked + Angular uses `OpaqueTokens` to register all of its non-class dependencies. + + Internally, the `Provider` turns both the string and the class type into an `OpaqueToken` + and keys its *token/provider* map with that. .l-main-section :marked @@ -560,77 +697,56 @@ include ../../../../_includes/_util-fns The Angular Dependency Injection is more capable than we've described. We can learn more about its advanced features, beginning with its support for nested injectors, in the - [Hierarchical Dependency Injection](./hierarchical-dependency-injection.html) chapter. - + [Hierarchical Dependency Injection](hierarchical-dependency-injection.html) chapter. + .l-main-section - + :marked - ### Appendix: Why we recommend one class per file - Developers expect one class per file. Multiple classes per file is confusing and is best avoided. - If we define every class in its own file, there is nothing in this note to worry about. - Move along! - - If we scorn this advice - and we add our `HeroService` class to the `HeroesComponent` file anyway, - **define the `HeroesComponent` last!** - If we put it define component before the service, - we'll get a runtime null reference error. - - To understand why, paste the following incorrect, ultra-simplified rendition of these two - classes into the [TypeScript playground](http://www.typescriptlang.org/Playground). - - ``` - class HeroesComponent { - static $providers=[HeroService] - } - - class HeroService { } - - alert(HeroesComponent.$providers) - ``` + ### Appendix: Working with injectors directly + We rarely work directly with an injector. + Here's an `InjectorComponent` that does. + ++makeExample('dependency-injection/ts/app/injector.component.ts', 'injector', + 'app/injector.component.ts') +:marked + Angular injects the component's own `Injector` which the component uses to acquire services. + The services themselves are not injected. They're retrieved via the injector. + + The `get` method throws an error if it can't resolve the requested service. + We can call `getOptional` instead, which we do in one case + to retrieve a service (`ROUS`) that isn't registered with this or any ancestor injector. + .l-sub-section :marked - The `HeroService` is incorrectly defined below the `HeroComponent`. + This technique is an example of the [Service Locator Pattern](https://en.wikipedia.org/wiki/Service_locator_pattern). + + We **avoid** this technique unless we genuinely need it. + It encourages a careless grab-bag approach such as we see here. + It's difficult to explain, understand, and test. + We can't know by inspecting the constructor what this class requires or what it will do. + It could acquire services from any ancestor component, not just its own. + We're forced to spelunk the implementation and hope for the best. + + Framework developers may take this approach when they + must acquire services generically and dynamically. - The `$providers` static property represents the metadata about the injected `HeroService` - that TypeScript compiler would add to the component class. - - The `alert` simulates the action of the Dependency Injector at runtime - when it attempts to create a `HeroesComponent`. +.l-main-section + :marked - Run it. The alert appears but displays nothing. - This is the equivalent of the null reference error thrown at runtime. + ### Appendix: Why we recommend one class per file + + Having multiple classes in the same file is confusing and best avoided. + Developers expect one class per file. Keep them happy. - We understand why when we review the generated JavaScript which looks like this: - ``` - var HeroesComponent = (function () { - function HeroesComponent() { - } - HeroesComponent.$providers = [HeroService]; - return HeroesComponent; - })(); + If we scorn this advice and, say, + combine our `HeroService` class with the `HeroesComponent` in the same file, + **define the component last!** + If we define the component before the service, + we'll get a runtime null reference error. - var HeroService = (function () { - function HeroService() { - } - return HeroService; - })(); - - alert(HeroesComponent.$providers); - ``` - - Notice that the TypeScript compiler turns classes into function expressions - assigned to variables. The value of the captured `HeroService` variable is undefined - when the `$providers` array is assigned. The `HeroService` variable gets its value too late - to be captured. - - Reverse the order of class definition so that the `HeroService` - appears before the `HeroesComponent` that requires it. - Run again. This time the alert displays the `HeroService` function definition. - - If we insist on defining the `HeroService` in the same file and insist on - defining the component first, Angular offers a way to make that work. - The `forwardRef()` method let's us reference a class - before it has been defined. - Learn more about this problem and the `forwardRef()` - in this [blog post](http://blog.thoughtram.io/angular/2015/09/03/forward-references-in-angular-2.html). +.l-sub-section + :marked + We actually can define the component first with the help of the `forwardRef()` method as explained + in this [blog post](http://blog.thoughtram.io/angular/2015/09/03/forward-references-in-angular-2.html). + But why flirt with trouble? + Avoid the problem altogether and define components and services in separate files.