989 lines
27 KiB
HTML
989 lines
27 KiB
HTML
|
<html lang="en"><head></head><body><form id="mainForm" method="post" action="http://plnkr.co/edit/?p=preview" target="_self"><input type="hidden" name="files[app/app-routing.module.ts]" value="import { NgModule } from '@angular/core';
|
||
|
import { Routes, RouterModule } from '@angular/router';
|
||
|
|
||
|
const routes: Routes = [];
|
||
|
|
||
|
@NgModule({
|
||
|
imports: [RouterModule.forRoot(routes)],
|
||
|
providers: [],
|
||
|
exports: [RouterModule]
|
||
|
})
|
||
|
export class AppRoutingModule {}
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/app.component.ts]" value="import { Component } from '@angular/core';
|
||
|
|
||
|
import { LoggerService } from './logger.service';
|
||
|
import { UserContextService } from './user-context.service';
|
||
|
import { UserService } from './user.service';
|
||
|
|
||
|
@Component({
|
||
|
moduleId: module.id,
|
||
|
selector: 'my-app',
|
||
|
templateUrl: './app.component.html',
|
||
|
providers: [ LoggerService, UserContextService, UserService ]
|
||
|
})
|
||
|
export class AppComponent {
|
||
|
|
||
|
private userId: number = 1;
|
||
|
|
||
|
constructor(logger: LoggerService, public userContext: UserContextService) {
|
||
|
userContext.loadUser(this.userId);
|
||
|
logger.logInfo('AppComponent initialized');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/app.module.ts]" value="import { BrowserModule } from '@angular/platform-browser';
|
||
|
import { FormsModule } from '@angular/forms';
|
||
|
import { HttpModule } from '@angular/http';
|
||
|
|
||
|
// import { AppRoutingModule } from './app-routing.module';
|
||
|
import { LocationStrategy,
|
||
|
HashLocationStrategy } from '@angular/common';
|
||
|
import { NgModule } from '@angular/core';
|
||
|
|
||
|
import { HeroData } from './hero-data';
|
||
|
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
|
||
|
|
||
|
|
||
|
import { AppComponent } from './app.component';
|
||
|
import { HeroBioComponent } from './hero-bio.component';
|
||
|
import { HeroBiosComponent,
|
||
|
HeroBiosAndContactsComponent } from './hero-bios.component';
|
||
|
import { HeroOfTheMonthComponent } from './hero-of-the-month.component';
|
||
|
import { HeroContactComponent } from './hero-contact.component';
|
||
|
import { HeroesBaseComponent,
|
||
|
SortedHeroesComponent } from './sorted-heroes.component';
|
||
|
import { HighlightDirective } from './highlight.directive';
|
||
|
import { ParentFinderComponent,
|
||
|
AlexComponent,
|
||
|
AliceComponent,
|
||
|
CarolComponent,
|
||
|
ChrisComponent,
|
||
|
CraigComponent,
|
||
|
CathyComponent,
|
||
|
BarryComponent,
|
||
|
BethComponent,
|
||
|
BobComponent } from './parent-finder.component';
|
||
|
|
||
|
const declarations = [
|
||
|
AppComponent,
|
||
|
HeroBiosComponent, HeroBiosAndContactsComponent, HeroBioComponent,
|
||
|
HeroesBaseComponent, SortedHeroesComponent,
|
||
|
HeroOfTheMonthComponent, HeroContactComponent,
|
||
|
HighlightDirective,
|
||
|
ParentFinderComponent,
|
||
|
];
|
||
|
|
||
|
const a_components = [AliceComponent, AlexComponent ];
|
||
|
|
||
|
const b_components = [ BarryComponent, BethComponent, BobComponent ];
|
||
|
|
||
|
const c_components = [
|
||
|
CarolComponent, ChrisComponent, CraigComponent,
|
||
|
CathyComponent
|
||
|
];
|
||
|
|
||
|
@NgModule({
|
||
|
imports: [
|
||
|
BrowserModule,
|
||
|
FormsModule,
|
||
|
HttpModule,
|
||
|
InMemoryWebApiModule.forRoot(HeroData)
|
||
|
// AppRoutingModule TODO: add routes
|
||
|
],
|
||
|
declarations: [
|
||
|
declarations,
|
||
|
a_components,
|
||
|
b_components,
|
||
|
c_components,
|
||
|
],
|
||
|
bootstrap: [ AppComponent ],
|
||
|
providers: [
|
||
|
{ provide: LocationStrategy, useClass: HashLocationStrategy }
|
||
|
]
|
||
|
})
|
||
|
export class AppModule { }
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/date-logger.service.ts]" value="/* tslint:disable:one-line:check-open-brace*/
|
||
|
import { Injectable } from '@angular/core';
|
||
|
|
||
|
import { LoggerService } from './logger.service';
|
||
|
|
||
|
// class used as a restricting interface (hides other public members)
|
||
|
export abstract class MinimalLogger {
|
||
|
logInfo: (msg: string) => void;
|
||
|
logs: string[];
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
// Transpiles to:
|
||
|
var MinimalLogger = (function () {
|
||
|
function MinimalLogger() {}
|
||
|
return MinimalLogger;
|
||
|
}());
|
||
|
exports("MinimalLogger", MinimalLogger);
|
||
|
*/
|
||
|
|
||
|
@Injectable()
|
||
|
export class DateLoggerService extends LoggerService implements MinimalLogger
|
||
|
{
|
||
|
logInfo(msg: any) { super.logInfo(stamp(msg)); }
|
||
|
logDebug(msg: any) { super.logInfo(stamp(msg)); }
|
||
|
logError(msg: any) { super.logError(stamp(msg)); }
|
||
|
}
|
||
|
|
||
|
function stamp(msg: any) { return msg + ' at ' + new Date(); }
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/hero-bio.component.ts]" value="import { Component, Input, OnInit } from '@angular/core';
|
||
|
|
||
|
import { HeroCacheService } from './hero-cache.service';
|
||
|
|
||
|
@Component({
|
||
|
selector: 'hero-bio',
|
||
|
template: `
|
||
|
<h4>{{hero.name}}</h4>
|
||
|
<ng-content></ng-content>
|
||
|
<textarea cols="25" [(ngModel)]="hero.description"></textarea>`,
|
||
|
providers: [HeroCacheService]
|
||
|
})
|
||
|
|
||
|
export class HeroBioComponent implements OnInit {
|
||
|
@Input() heroId: number;
|
||
|
|
||
|
constructor(private heroCache: HeroCacheService) { }
|
||
|
|
||
|
ngOnInit() { this.heroCache.fetchCachedHero(this.heroId); }
|
||
|
|
||
|
get hero() { return this.heroCache.hero; }
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/hero-bios.component.ts]" value="import { Component } from '@angular/core';
|
||
|
|
||
|
import { HeroService } from './hero.service';
|
||
|
import { LoggerService } from './logger.service';
|
||
|
|
||
|
//////// HeroBiosComponent ////
|
||
|
@Component({
|
||
|
selector: 'hero-bios',
|
||
|
template: `
|
||
|
<hero-bio [heroId]="1"></hero-bio>
|
||
|
<hero-bio [heroId]="2"></hero-bio>
|
||
|
<hero-bio [heroId]="3"></hero-bio>`,
|
||
|
providers: [HeroService]
|
||
|
})
|
||
|
export class HeroBiosComponent {
|
||
|
constructor(logger: LoggerService) {
|
||
|
logger.logInfo('Creating HeroBiosComponent');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//////// HeroBiosAndContactsComponent ////
|
||
|
@Component({
|
||
|
selector: 'hero-bios-and-contacts',
|
||
|
template: `
|
||
|
<hero-bio [heroId]="1"> <hero-contact></hero-contact> </hero-bio>
|
||
|
<hero-bio [heroId]="2"> <hero-contact></hero-contact> </hero-bio>
|
||
|
<hero-bio [heroId]="3"> <hero-contact></hero-contact> </hero-bio>`,
|
||
|
providers: [HeroService]
|
||
|
})
|
||
|
export class HeroBiosAndContactsComponent {
|
||
|
constructor(logger: LoggerService) {
|
||
|
logger.logInfo('Creating HeroBiosAndContactsComponent');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/hero-cache.service.ts]" value="import { Injectable } from '@angular/core';
|
||
|
|
||
|
import { Hero } from './hero';
|
||
|
import { HeroService } from './hero.service';
|
||
|
|
||
|
@Injectable()
|
||
|
export class HeroCacheService {
|
||
|
hero: Hero;
|
||
|
constructor(private heroService: HeroService) {}
|
||
|
|
||
|
fetchCachedHero(id: number) {
|
||
|
if (!this.hero) {
|
||
|
this.hero = this.heroService.getHeroById(id);
|
||
|
}
|
||
|
return this.hero;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/hero-contact.component.ts]" value="import { Component, Host, Optional } from '@angular/core';
|
||
|
|
||
|
import { HeroCacheService } from './hero-cache.service';
|
||
|
import { LoggerService } from './logger.service';
|
||
|
|
||
|
@Component({
|
||
|
selector: 'hero-contact',
|
||
|
template: `
|
||
|
<div>Phone #: {{phoneNumber}}
|
||
|
<span *ngIf="hasLogger">!!!</span></div>`
|
||
|
})
|
||
|
export class HeroContactComponent {
|
||
|
|
||
|
hasLogger = false;
|
||
|
|
||
|
constructor(
|
||
|
@Host() // limit to the host component's instance of the HeroCacheService
|
||
|
private heroCache: HeroCacheService,
|
||
|
|
||
|
@Host() // limit search for logger; hides the application-wide logger
|
||
|
@Optional() // ok if the logger doesn't exist
|
||
|
private loggerService: LoggerService
|
||
|
) {
|
||
|
if (loggerService) {
|
||
|
this.hasLogger = true;
|
||
|
loggerService.logInfo('HeroContactComponent can log!');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
get phoneNumber() { return this.heroCache.hero.phone; }
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/hero-data.ts]" value="import { Hero } from './hero';
|
||
|
|
||
|
export class HeroData {
|
||
|
createDb() {
|
||
|
let heroes = [
|
||
|
new Hero(1, 'Windstorm'),
|
||
|
new Hero(2, 'Bombasto'),
|
||
|
new Hero(3, 'Magneta'),
|
||
|
new Hero(4, 'Tornado')
|
||
|
];
|
||
|
return {heroes};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/hero-of-the-month.component.ts]" value="/* tslint:disable:one-line:check-open-brace*/
|
||
|
import { OpaqueToken } from '@angular/core';
|
||
|
|
||
|
export const TITLE = new OpaqueToken('title');
|
||
|
|
||
|
import { Component, Inject } from '@angular/core';
|
||
|
|
||
|
import { DateLoggerService,
|
||
|
MinimalLogger } from './date-logger.service';
|
||
|
import { Hero } from './hero';
|
||
|
import { HeroService } from './hero.service';
|
||
|
import { LoggerService } from './logger.service';
|
||
|
import { RUNNERS_UP,
|
||
|
runnersUpFactory } from './runners-up';
|
||
|
|
||
|
const someHero = new Hero(42, 'Magma', 'Had a great month!', '555-555-5555');
|
||
|
|
||
|
const template = `
|
||
|
<h3>{{title}}</h3>
|
||
|
<div>Winner: <strong>{{heroOfTheMonth.name}}</strong></div>
|
||
|
<div>Reason for award: <strong>{{heroOfTheMonth.description}}</strong></div>
|
||
|
<div>Runners-up: <strong id="rups1">{{runnersUp}}</strong></div>
|
||
|
|
||
|
<p>Logs:</p>
|
||
|
<div id="logs">
|
||
|
<div *ngFor="let log of logs">{{log}}</div>
|
||
|
</div>
|
||
|
`;
|
||
|
|
||
|
@Component({
|
||
|
selector: 'hero-of-the-month',
|
||
|
template: template,
|
||
|
providers: [
|
||
|
{ provide: Hero, useValue: someHero },
|
||
|
{ provide: TITLE, useValue: 'Hero of the Month' },
|
||
|
{ provide: HeroService, useClass: HeroService },
|
||
|
{ provide: LoggerService, useClass: DateLoggerService },
|
||
|
{ provide: MinimalLogger, useExisting: LoggerService },
|
||
|
{ provide: RUNNERS_UP, useFactory: runnersUpFactory(2), deps: [Hero, HeroService] }
|
||
|
]
|
||
|
})
|
||
|
export class HeroOfTheMonthComponent {
|
||
|
logs: string[] = [];
|
||
|
|
||
|
constructor(
|
||
|
logger: MinimalLogger,
|
||
|
public heroOfTheMonth: Hero,
|
||
|
@Inject(RUNNERS_UP) public runnersUp: string,
|
||
|
@Inject(TITLE) public title: string)
|
||
|
{
|
||
|
this.logs = logger.logs;
|
||
|
logger.logInfo('starting up');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/hero.service.ts]" value="import { Injectable } from '@angular/core';
|
||
|
import { Hero } from './hero';
|
||
|
|
||
|
@Injectable()
|
||
|
export class HeroService {
|
||
|
|
||
|
// TODO move to database
|
||
|
private heroes: Array<Hero> = [
|
||
|
new Hero(1, 'RubberMan', 'Hero of many talents', '123-456-7899'),
|
||
|
new Hero(2, 'Magma', 'Hero of all trades', '555-555-5555'),
|
||
|
new Hero(3, 'Mr. Nice', 'The name says it all', '111-222-3333')
|
||
|
];
|
||
|
|
||
|
getHeroById(id: number): Hero {
|
||
|
return this.heroes.find(hero => hero.id === id);
|
||
|
}
|
||
|
|
||
|
getAllHeroes(): Array<Hero> {
|
||
|
return this.heroes;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/hero.ts]" value="export class Hero {
|
||
|
constructor(
|
||
|
public id: number,
|
||
|
public name: string,
|
||
|
public description?: string,
|
||
|
public phone?: string) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/highlight.directive.ts]" value="import { Directive, ElementRef, HostListener, Input } from '@angular/core';
|
||
|
|
||
|
@Directive({
|
||
|
selector: '[myHighlight]'
|
||
|
})
|
||
|
export class HighlightDirective {
|
||
|
|
||
|
@Input('myHighlight') highlightColor: string;
|
||
|
|
||
|
private el: HTMLElement;
|
||
|
|
||
|
constructor(el: ElementRef) {
|
||
|
this.el = el.nativeElement;
|
||
|
}
|
||
|
|
||
|
@HostListener('mouseenter') onMouseEnter() {
|
||
|
this.highlight(this.highlightColor || 'cyan');
|
||
|
}
|
||
|
|
||
|
@HostListener('mouseleave') onMouseLeave() {
|
||
|
this.highlight(null);
|
||
|
}
|
||
|
|
||
|
private highlight(color: string) {
|
||
|
this.el.style.backgroundColor = color;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/logger.service.ts]" value="import { Injectable } from '@angular/core';
|
||
|
|
||
|
@Injectable()
|
||
|
export class LoggerService {
|
||
|
logs: string[] = [];
|
||
|
|
||
|
logInfo(msg: any) { this.log(`INFO: ${msg}`); }
|
||
|
logDebug(msg: any) { this.log(`DEBUG: ${msg}`); }
|
||
|
logError(msg: any) { this.log(`ERROR: ${msg}`, true); }
|
||
|
|
||
|
private log(msg: any, isErr = false) {
|
||
|
this.logs.push(msg);
|
||
|
isErr ? console.error(msg) : console.log(msg);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/parent-finder.component.ts]" value="/* tslint:disable:no-unused-variable component-selector-name one-line check-open-brace */
|
||
|
/* tslint:disable:*/
|
||
|
import { Component, forwardRef, Optional, SkipSelf } from '@angular/core';
|
||
|
|
||
|
// A component base class (see AlexComponent)
|
||
|
export abstract class Base { name = 'Count Basie'; }
|
||
|
|
||
|
// Marker class, used as an interface
|
||
|
export abstract class Parent { name: string; }
|
||
|
|
||
|
const DifferentParent = Parent;
|
||
|
|
||
|
// Helper method to provide the current component instance in the name of a `parentType`.
|
||
|
// The `parentType` defaults to `Parent` when omitting the second parameter.
|
||
|
const provideParent =
|
||
|
(component: any, parentType?: any) => {
|
||
|
return { provide: parentType || Parent, useExisting: forwardRef(() => component) };
|
||
|
};
|
||
|
|
||
|
// Simpler syntax version that always provides the component in the name of `Parent`.
|
||
|
const provideTheParent =
|
||
|
(component: any) => {
|
||
|
return { provide: Parent, useExisting: forwardRef(() => component) };
|
||
|
};
|
||
|
|
||
|
|
||
|
///////// C - Child //////////
|
||
|
const templateC = `
|
||
|
<div class="c">
|
||
|
<h3>{{name}}</h3>
|
||
|
<p>My parent is {{parent?.name}}</p>
|
||
|
</div>`;
|
||
|
|
||
|
@Component({
|
||
|
selector: 'carol',
|
||
|
template: templateC
|
||
|
})
|
||
|
export class CarolComponent {
|
||
|
name= 'Carol';
|
||
|
constructor( @Optional() public parent: Parent ) { }
|
||
|
}
|
||
|
|
||
|
@Component({
|
||
|
selector: 'chris',
|
||
|
template: templateC
|
||
|
})
|
||
|
export class ChrisComponent {
|
||
|
name= 'Chris';
|
||
|
constructor( @Optional() public parent: Parent ) { }
|
||
|
}
|
||
|
|
||
|
////// Craig ///////////
|
||
|
/**
|
||
|
* Show we cannot inject a parent by its base class.
|
||
|
*/
|
||
|
@Component({
|
||
|
selector: 'craig',
|
||
|
template: `
|
||
|
<div class="c">
|
||
|
<h3>Craig</h3>
|
||
|
{{alex ? 'Found' : 'Did not find'}} Alex via the base class.
|
||
|
</div>`
|
||
|
})
|
||
|
export class CraigComponent {
|
||
|
constructor( @Optional() public alex: Base ) { }
|
||
|
}
|
||
|
|
||
|
//////// B - Parent /////////
|
||
|
const templateB = `
|
||
|
<div class="b">
|
||
|
<div>
|
||
|
<h3>{{name}}</h3>
|
||
|
<p>My parent is {{parent?.name}}</p>
|
||
|
</div>
|
||
|
<carol></carol>
|
||
|
<chris></chris>
|
||
|
</div>`;
|
||
|
|
||
|
@Component({
|
||
|
selector: 'barry',
|
||
|
template: templateB,
|
||
|
providers: [{ provide: Parent, useExisting: forwardRef(() => BarryComponent) }]
|
||
|
})
|
||
|
export class BarryComponent implements Parent {
|
||
|
name = 'Barry';
|
||
|
constructor( @SkipSelf() @Optional() public parent: Parent ) { }
|
||
|
}
|
||
|
|
||
|
@Component({
|
||
|
selector: 'bob',
|
||
|
template: templateB,
|
||
|
providers: [ provideParent(BobComponent) ]
|
||
|
})
|
||
|
export class BobComponent implements Parent {
|
||
|
name= 'Bob';
|
||
|
constructor( @SkipSelf() @Optional() public parent: Parent ) { }
|
||
|
}
|
||
|
|
||
|
@Component({
|
||
|
selector: 'beth',
|
||
|
template: templateB,
|
||
|
providers: [ provideParent(BethComponent, DifferentParent) ]
|
||
|
})
|
||
|
export class BethComponent implements Parent {
|
||
|
name= 'Beth';
|
||
|
constructor( @SkipSelf() @Optional() public parent: Parent ) { }
|
||
|
}
|
||
|
|
||
|
///////// A - Grandparent //////
|
||
|
|
||
|
@Component({
|
||
|
selector: 'alex',
|
||
|
template: `
|
||
|
<div class="a">
|
||
|
<h3>{{name}}</h3>
|
||
|
<cathy></cathy>
|
||
|
<craig></craig>
|
||
|
<carol></carol>
|
||
|
</div>`,
|
||
|
providers: [{ provide: Parent, useExisting: forwardRef(() => AlexComponent) }],
|
||
|
})
|
||
|
// Todo: Add `... implements Parent` to class signature
|
||
|
export class AlexComponent extends Base
|
||
|
{
|
||
|
name= 'Alex';
|
||
|
}
|
||
|
|
||
|
/////
|
||
|
|
||
|
@Component({
|
||
|
selector: 'alice',
|
||
|
template: `
|
||
|
<div class="a">
|
||
|
<h3>{{name}}</h3>
|
||
|
<barry></barry>
|
||
|
<beth></beth>
|
||
|
<bob></bob>
|
||
|
<carol></carol>
|
||
|
</div> `,
|
||
|
providers: [ provideParent(AliceComponent) ]
|
||
|
})
|
||
|
export class AliceComponent implements Parent
|
||
|
{
|
||
|
name= 'Alice';
|
||
|
}
|
||
|
|
||
|
////// Cathy ///////////
|
||
|
/**
|
||
|
* Show we can inject a parent by component type
|
||
|
*/
|
||
|
@Component({
|
||
|
selector: 'cathy',
|
||
|
template: `
|
||
|
<div class="c">
|
||
|
<h3>Cathy</h3>
|
||
|
{{alex ? 'Found' : 'Did not find'}} Alex via the component class.<br>
|
||
|
</div>`
|
||
|
})
|
||
|
export class CathyComponent {
|
||
|
constructor( @Optional() public alex: AlexComponent ) { }
|
||
|
}
|
||
|
|
||
|
///////// ParentFinder //////
|
||
|
@Component({
|
||
|
selector: 'parent-finder',
|
||
|
template: `
|
||
|
<h2>Parent Finder</h2>
|
||
|
<alex></alex>
|
||
|
<alice></alice>`
|
||
|
})
|
||
|
export class ParentFinderComponent { }
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/runners-up.ts]" value="import { OpaqueToken } from '@angular/core';
|
||
|
|
||
|
import { Hero } from './hero';
|
||
|
import { HeroService } from './hero.service';
|
||
|
|
||
|
export const RUNNERS_UP = new OpaqueToken('RunnersUp');
|
||
|
|
||
|
export function runnersUpFactory(take: number) {
|
||
|
return (winner: Hero, heroService: HeroService): string => {
|
||
|
/* ... */
|
||
|
return heroService
|
||
|
.getAllHeroes()
|
||
|
.filter((hero) => hero.name !== winner.name)
|
||
|
.map(hero => hero.name)
|
||
|
.slice(0, Math.max(0, take))
|
||
|
.join(', ');
|
||
|
};
|
||
|
};
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/sorted-heroes.component.ts]" value="import { Component, OnInit } from '@angular/core';
|
||
|
|
||
|
import { Hero } from './hero';
|
||
|
import { HeroService } from './hero.service';
|
||
|
|
||
|
/////// HeroesBaseComponent /////
|
||
|
@Component({
|
||
|
selector: 'unsorted-heroes',
|
||
|
template: `<div *ngFor="let hero of heroes">{{hero.name}}</div>`,
|
||
|
providers: [HeroService]
|
||
|
})
|
||
|
export class HeroesBaseComponent implements OnInit {
|
||
|
constructor(private heroService: HeroService) { }
|
||
|
|
||
|
heroes: Array<Hero>;
|
||
|
|
||
|
ngOnInit() {
|
||
|
this.heroes = this.heroService.getAllHeroes();
|
||
|
this.afterGetHeroes();
|
||
|
}
|
||
|
|
||
|
// Post-process heroes in derived class override.
|
||
|
protected afterGetHeroes() {}
|
||
|
|
||
|
}
|
||
|
|
||
|
/////// SortedHeroesComponent /////
|
||
|
@Component({
|
||
|
selector: 'sorted-heroes',
|
||
|
template: `<div *ngFor="let hero of heroes">{{hero.name}}</div>`,
|
||
|
providers: [HeroService]
|
||
|
})
|
||
|
export class SortedHeroesComponent extends HeroesBaseComponent {
|
||
|
constructor(heroService: HeroService) {
|
||
|
super(heroService);
|
||
|
}
|
||
|
|
||
|
protected afterGetHeroes() {
|
||
|
this.heroes = this.heroes.sort((h1, h2) => {
|
||
|
return h1.name < h2.name ? -1 :
|
||
|
(h1.name > h2.name ? 1 : 0);
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/user-context.service.ts]" value="import { Injectable } from '@angular/core';
|
||
|
|
||
|
import { LoggerService } from './logger.service';
|
||
|
import { UserService } from './user.service';
|
||
|
|
||
|
@Injectable()
|
||
|
export class UserContextService {
|
||
|
name: string;
|
||
|
role: string;
|
||
|
loggedInSince: Date;
|
||
|
|
||
|
constructor(private userService: UserService, private loggerService: LoggerService) {
|
||
|
this.loggedInSince = new Date();
|
||
|
}
|
||
|
|
||
|
loadUser(userId: number) {
|
||
|
let user = this.userService.getUserById(userId);
|
||
|
this.name = user.name;
|
||
|
this.role = user.role;
|
||
|
|
||
|
this.loggerService.logDebug('loaded User');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/user.service.ts]" value="import { Injectable } from '@angular/core';
|
||
|
|
||
|
@Injectable()
|
||
|
export class UserService {
|
||
|
|
||
|
getUserById(userId: number): any {
|
||
|
return {name: 'Bombasto', role: 'Admin'};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[main.ts]" value="import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
||
|
import { AppModule } from './app/app.module';
|
||
|
|
||
|
platformBrowserDynamic().bootstrapModule(AppModule);
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[sample.css]" value=".di-component{
|
||
|
padding: 10px;
|
||
|
width:300px;
|
||
|
margin-bottom: 10px;
|
||
|
}
|
||
|
div[myHighlight] {
|
||
|
padding: 2px 8px;
|
||
|
}
|
||
|
|
||
|
/* Parent Finder */
|
||
|
.a, .b, .c {
|
||
|
margin: 6px 2px 6px;
|
||
|
padding: 4px 6px;
|
||
|
}
|
||
|
.a {
|
||
|
border: solid 2px black;
|
||
|
}
|
||
|
.b {
|
||
|
background: lightblue;
|
||
|
border: solid 1px darkblue;
|
||
|
display: flex;
|
||
|
}
|
||
|
.c {
|
||
|
background: pink;
|
||
|
border: solid 1px red;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[styles.css]" value="/* Master Styles */
|
||
|
h1 {
|
||
|
color: #369;
|
||
|
font-family: Arial, Helvetica, sans-serif;
|
||
|
font-size: 250%;
|
||
|
}
|
||
|
h2, h3 {
|
||
|
color: #444;
|
||
|
font-family: Arial, Helvetica, sans-serif;
|
||
|
font-weight: lighter;
|
||
|
}
|
||
|
body {
|
||
|
margin: 2em;
|
||
|
}
|
||
|
body, input[text], button {
|
||
|
color: #888;
|
||
|
font-family: Cambria, Georgia;
|
||
|
}
|
||
|
a {
|
||
|
cursor: pointer;
|
||
|
cursor: hand;
|
||
|
}
|
||
|
button {
|
||
|
font-family: Arial;
|
||
|
background-color: #eee;
|
||
|
border: none;
|
||
|
padding: 5px 10px;
|
||
|
border-radius: 4px;
|
||
|
cursor: pointer;
|
||
|
cursor: hand;
|
||
|
}
|
||
|
button:hover {
|
||
|
background-color: #cfd8dc;
|
||
|
}
|
||
|
button:disabled {
|
||
|
background-color: #eee;
|
||
|
color: #aaa;
|
||
|
cursor: auto;
|
||
|
}
|
||
|
|
||
|
/* Navigation link styles */
|
||
|
nav a {
|
||
|
padding: 5px 10px;
|
||
|
text-decoration: none;
|
||
|
margin-right: 10px;
|
||
|
margin-top: 10px;
|
||
|
display: inline-block;
|
||
|
background-color: #eee;
|
||
|
border-radius: 4px;
|
||
|
}
|
||
|
nav a:visited, a:link {
|
||
|
color: #607D8B;
|
||
|
}
|
||
|
nav a:hover {
|
||
|
color: #039be5;
|
||
|
background-color: #CFD8DC;
|
||
|
}
|
||
|
nav a.active {
|
||
|
color: #039be5;
|
||
|
}
|
||
|
|
||
|
/* items class */
|
||
|
.items {
|
||
|
margin: 0 0 2em 0;
|
||
|
list-style-type: none;
|
||
|
padding: 0;
|
||
|
width: 24em;
|
||
|
}
|
||
|
.items li {
|
||
|
cursor: pointer;
|
||
|
position: relative;
|
||
|
left: 0;
|
||
|
background-color: #EEE;
|
||
|
margin: .5em;
|
||
|
padding: .3em 0;
|
||
|
height: 1.6em;
|
||
|
border-radius: 4px;
|
||
|
}
|
||
|
.items li:hover {
|
||
|
color: #607D8B;
|
||
|
background-color: #DDD;
|
||
|
left: .1em;
|
||
|
}
|
||
|
.items li.selected {
|
||
|
background-color: #CFD8DC;
|
||
|
color: white;
|
||
|
}
|
||
|
.items li.selected:hover {
|
||
|
background-color: #BBD8DC;
|
||
|
}
|
||
|
.items .text {
|
||
|
position: relative;
|
||
|
top: -3px;
|
||
|
}
|
||
|
.items .badge {
|
||
|
display: inline-block;
|
||
|
font-size: small;
|
||
|
color: white;
|
||
|
padding: 0.8em 0.7em 0 0.7em;
|
||
|
background-color: #607D8B;
|
||
|
line-height: 1em;
|
||
|
position: relative;
|
||
|
left: -1px;
|
||
|
top: -4px;
|
||
|
height: 1.8em;
|
||
|
margin-right: .8em;
|
||
|
border-radius: 4px 0 0 4px;
|
||
|
}
|
||
|
/* everywhere else */
|
||
|
* {
|
||
|
font-family: Arial, Helvetica, sans-serif;
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
*/"><input type="hidden" name="files[app/app.component.html]" value="<h1>DI Cookbook</h1>
|
||
|
<div class="di-component">
|
||
|
<h3>Logged in user</h3>
|
||
|
<div>Name: {{userContext.name}}</div>
|
||
|
<div>Role: {{userContext.role}}</div>
|
||
|
</div>
|
||
|
|
||
|
<div class="di-component">
|
||
|
<h3>Hero Bios</h3>
|
||
|
<hero-bios></hero-bios>
|
||
|
</div>
|
||
|
|
||
|
<div id="highlight" class="di-component" myHighlight>
|
||
|
<h3>Hero Bios and Contacts</h3>
|
||
|
<div myHighlight="yellow">
|
||
|
<hero-bios-and-contacts></hero-bios-and-contacts>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
<div class="di-component">
|
||
|
<hero-of-the-month></hero-of-the-month>
|
||
|
</div>
|
||
|
|
||
|
<div class="di-component">
|
||
|
<h3>Unsorted Heroes</h3>
|
||
|
<unsorted-heroes></unsorted-heroes>
|
||
|
</div>
|
||
|
|
||
|
<div class="di-component">
|
||
|
<h3>Sorted Heroes</h3>
|
||
|
<sorted-heroes></sorted-heroes>
|
||
|
</div>
|
||
|
|
||
|
<div class="di-component">
|
||
|
<parent-finder></parent-finder>
|
||
|
</div>
|
||
|
|
||
|
|
||
|
<!--
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
-->"><input type="hidden" name="files[index.html]" value="<!DOCTYPE html>
|
||
|
<html>
|
||
|
<head>
|
||
|
<script>document.write('<base href="' + document.location + '" />');</script>
|
||
|
<meta charset="UTF-8">
|
||
|
<title>Dependency Injection</title>
|
||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
|
<link rel="stylesheet" href="styles.css">
|
||
|
<link rel="stylesheet" href="sample.css">
|
||
|
|
||
|
<!-- Polyfills -->
|
||
|
<script src="https://unpkg.com/core-js/client/shim.min.js"></script>
|
||
|
|
||
|
<script src="https://unpkg.com/zone.js@0.7.4?main=browser"></script>
|
||
|
<script src="https://unpkg.com/systemjs@0.19.39/dist/system.src.js"></script>
|
||
|
|
||
|
<script src="https://cdn.rawgit.com/angular/angular.io/b3c65a9/public/docs/_examples/_boilerplate/systemjs.config.web.js"></script>
|
||
|
<script>
|
||
|
System.import('main.js').catch(function(err){ console.error(err); });
|
||
|
</script>
|
||
|
</head>
|
||
|
|
||
|
<body>
|
||
|
<my-app>Loading app...</my-app>
|
||
|
</body>
|
||
|
|
||
|
</html>
|
||
|
|
||
|
|
||
|
<!--
|
||
|
Copyright 2016 Google Inc. All Rights Reserved.
|
||
|
Use of this source code is governed by an MIT-style license that
|
||
|
can be found in the LICENSE file at http://angular.io/license
|
||
|
-->"><input type="hidden" name="tags[0]" value="angular"><input type="hidden" name="tags[1]" value="example"><input type="hidden" name="tags[2]" value="cookbook"><input type="hidden" name="private" value="true"><input type="hidden" name="description" value="Angular Example - Dependency Injection"></form><script>document.getElementById("mainForm").submit();</script></body></html>
|