Skip to content

Commit ee2d64a

Browse files
committed
docs:Creating DI cookbook
working on text text working on bios more text added plunker added sorted list text formatting hero of the month use labels hero of the month formatting text fixed text added host optional formating text element-ref text updates text updates images fixed image added test text
1 parent 1184b57 commit ee2d64a

28 files changed

+544
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
describe('Dependency Injection', function () {
2+
3+
beforeAll(function () {
4+
browser.get('');
5+
});
6+
7+
it('should render DI samples', function () {
8+
var loggedInUser = element.all(by.xpath('//h3[text()="Logged in user"]')).get(0);
9+
expect(loggedInUser).toBeDefined();
10+
11+
loggedInUser = element.all(by.xpath('//div[text()="Name: Bombasto"]')).get(0);
12+
expect(loggedInUser).toBeDefined();
13+
14+
var sortedHeroes = element.all(by.xpath('//h3[text()="Sorted Heroes" and position()=1]')).get(0);
15+
expect(sortedHeroes).toBeDefined();
16+
17+
var sortedHero = element.all(by.xpath('//sorted-heroes/[text()="Mr. Nice" and position()=2]')).get(0);
18+
expect(sortedHero).toBeDefined();
19+
20+
sortedHero = element.all(by.xpath('//sorted-heroes/[text()="RubberMan" and position()=3]')).get(0);
21+
expect(sortedHero).toBeDefined();
22+
23+
sortedHero = element.all(by.xpath('//sorted-heroes/[text()="Magma"]')).get(0);
24+
expect(sortedHero).toBeDefined();
25+
26+
var heroOfTheMonth = element.all(by.xpath('//h3[text()="Hero of the month"]')).get(0);
27+
expect(heroOfTheMonth).toBeDefined();
28+
29+
var heroBios = element.all(by.xpath('//h3[text()="Hero Bios"]')).get(0);
30+
expect(heroBios).toBeDefined();
31+
32+
var magmaText = element.all(by.xpath('//textarea[text()="Hero of all trades"]')).get(0);
33+
expect(magmaText).toBeDefined();
34+
35+
var magmaPhone = element.all(by.xpath('//div[text()="Phone #: 555-555-5555"]')).get(0);
36+
expect(magmaPhone).toBeDefined();
37+
38+
var runnersUp = element.all(by.xpath('//h4[text()="Other candidates RubberMan, Mr. Nice"]')).get(0);
39+
expect(runnersUp).toBeDefined();
40+
});
41+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/*.js
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// #docregion
2+
import {Component,OnInit} from 'angular2/core';
3+
import {LoggerService} from './logger.service';
4+
import {UserContext} from './user-context.service';
5+
import {Heroes} from './hero-bios.component';
6+
import {SortedHeroes} from './sorted-heroes.component';
7+
import {HeroOfTheMonth} from './hero-of-the-month.component';
8+
9+
@Component({
10+
selector: 'my-app',
11+
directives:[Heroes,SortedHeroes,HeroOfTheMonth],
12+
template:
13+
`<h1>DI Components</h1>
14+
<div class="di-component">
15+
<h3>Logged in user</h3>
16+
<div>Name: {{_userContext.name}}</div>
17+
<div>Role: {{_userContext.role}}</div>
18+
</div>
19+
20+
<div class="di-component">
21+
<h3>Sorted Heroes</h3>
22+
<sorted-heroes></sorted-heroes>
23+
</div>
24+
25+
<div class="di-component">
26+
<h3>Hero of the month</h3>
27+
<hero-of-the-month></hero-of-the-month>
28+
</div>
29+
30+
<div class="di-component">
31+
<h3>Hero Bios</h3>
32+
<hero-bios></hero-bios>
33+
</div>`
34+
})
35+
36+
export class AppComponent implements OnInit {
37+
38+
private userId:number = 1;
39+
40+
constructor(private _logger:LoggerService, private _userContext:UserContext){
41+
this._userContext.loadUser(this.userId);
42+
}
43+
44+
ngOnInit(){
45+
this._logger.logInfo('AppComponent initialized');
46+
}
47+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// #docregion
2+
import {Component,Input,OnInit} from 'angular2/core';
3+
import {Hero} from './hero';
4+
import {HeroService} from './hero.service';
5+
6+
@Component({
7+
selector:'bio',
8+
template:`<h4>{{hero.name}}</h4>
9+
<div>
10+
<ng-content></ng-content>
11+
<textarea cols="25" [(ngModel)]="hero.description">{{hero.description}}</textarea>
12+
</div>`,
13+
providers:[HeroService]
14+
})
15+
16+
export class Bio implements OnInit{
17+
18+
@Input() heroIndex:number;
19+
private hero:Hero;
20+
21+
constructor(private _heroService:HeroService){
22+
}
23+
24+
ngOnInit(){
25+
this.hero = this._heroService.getHeroById(this.heroIndex);
26+
}
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// #docregion
2+
import {Component, Host, Optional, OnInit, ElementRef} from 'angular2/core';
3+
4+
import {HeroService} from './hero.service';
5+
6+
@Component({
7+
selector:'contact-details',
8+
template:'<div>Phone #: {{phoneNumber}}</div>'
9+
})
10+
11+
export class ContactDetails implements OnInit{
12+
13+
private phoneNumber:string;
14+
constructor(@Optional() @Host() private _heroService:HeroService, elementRef:ElementRef){
15+
let nativeElement = elementRef.nativeElement;
16+
}
17+
18+
ngOnInit(){
19+
if(this._heroService){
20+
this.phoneNumber = this._heroService.currentHero.phone;
21+
}
22+
}
23+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// #docregion
2+
export class DateLoggerService{
3+
logInfo(msg:string){
4+
console.log(new Date().toString() + ` INFO: ${msg}`);
5+
}
6+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// #docregion
2+
import {Component} from 'angular2/core';
3+
import {Bio} from './bio.component';
4+
import {ContactDetails} from './contact-details.component';
5+
6+
@Component({
7+
template:`<div>
8+
<bio [heroIndex]="0">
9+
<contact-details></contact-details>
10+
</bio>
11+
</div>
12+
<div>
13+
<bio [heroIndex]="1">
14+
<contact-details></contact-details>
15+
</bio>
16+
</div>
17+
<div>
18+
<bio [heroIndex]="2">
19+
<contact-details></contact-details>
20+
</bio>
21+
</div>`,
22+
selector:'hero-bios',
23+
directives:[Bio,ContactDetails]
24+
})
25+
26+
export class Heroes{
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// #docregion
2+
import {Component,provide} from 'angular2/core';
3+
import {LoggerService} from './logger.service';
4+
import {Hero} from './hero';
5+
import {HeroService} from './hero.service';
6+
import {DateLoggerService} from './date-logger.service';
7+
import {RunnersUp} from './runners-up';
8+
import {runnersUpFactory} from './runners-up-provider.service';
9+
10+
@Component({
11+
selector:'hero-of-the-month',
12+
template:`<div>Winner: <strong>{{_heroOfTheMonth.name}}</strong></div>
13+
<div>Reason for award: <strong>{{_heroOfTheMonth.description}}</strong></div>
14+
<h4>Other candidates {{_runnersUp.names}}</h4>`,
15+
16+
providers:[
17+
HeroService,
18+
provide(Hero, {useValue:new Hero('Magma','Had a great month!','555-555-5555')}),
19+
provide(LoggerService, {useClass:DateLoggerService}),
20+
provide(RunnersUp, {useFactory:runnersUpFactory, deps:[Hero, HeroService]})
21+
]
22+
})
23+
24+
export class HeroOfTheMonth{
25+
constructor(logger:LoggerService, private _heroOfTheMonth:Hero, private _runnersUp:RunnersUp){
26+
logger.logInfo('starting up');
27+
}
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// #docregion
2+
import {Hero} from './hero';
3+
4+
export class HeroService{
5+
6+
currentHero:Hero;
7+
8+
//TODO move to database
9+
private _heros:Array<Hero> = [new Hero('RubberMan','Hero of many talents', '123-456-7899'),
10+
new Hero('Magma','Hero of all trades', '555-555-5555'),
11+
new Hero('Mr. Nice','The name says it all','111-222-3333')];
12+
13+
getHeroById(index:number):Hero{
14+
if(!this.currentHero){
15+
let heroes = this.getAllHeroes();
16+
this.currentHero = heroes[index];
17+
}
18+
19+
return this.currentHero;
20+
}
21+
22+
getAllHeroes():Array<Hero>{
23+
return this._heros;
24+
}
25+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// #docregion
2+
export class Hero{
3+
constructor(public name:string, public description:string, public phone:string){
4+
}
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// #docregion
2+
export class LoggerService{
3+
4+
logInfo(msg:string){
5+
console.log(`INFO: ${msg}`);
6+
}
7+
8+
logDebug(msg:string){
9+
console.log(`DEBUG: ${msg}`);
10+
}
11+
12+
logError(msg:string){
13+
console.log(`ERROR: ${msg}`);
14+
}
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// #docregion
2+
import {bootstrap} from 'angular2/platform/browser';
3+
import {AppComponent} from './app.component';
4+
import {LoggerService} from './logger.service';
5+
import {UserContext} from './user-context.service';
6+
import {UserService} from './user.service';
7+
8+
bootstrap(AppComponent, [LoggerService,UserService,UserContext])
9+
.catch((err:any) => console.error(err));
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// #docregion
2+
import {Hero} from './hero';
3+
import {HeroService} from './hero.service';
4+
import {RunnersUp} from './runners-up';
5+
6+
export const runnersUpFactory = (winner:Hero, heroService:HeroService) => {
7+
let names:string = heroService.getAllHeroes()
8+
.filter((hero) => hero.name !== winner.name)
9+
.map((hero) => hero.name).join(', ');
10+
11+
return new RunnersUp(names);
12+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// #docregion
2+
export class RunnersUp{
3+
names:string;
4+
5+
constructor(names:string){
6+
this.names = names;
7+
}
8+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// #docregion
2+
import {HeroService} from './hero.service';
3+
import {Hero} from './hero';
4+
5+
export class SortedHeroesBase{
6+
7+
sortedHeroes:Array<Hero>;
8+
9+
constructor(private heroService:HeroService){
10+
this.sortedHeroes = heroService.getAllHeroes();
11+
12+
this.sortedHeroes.sort((h1,h2) => {
13+
if(h1.name < h2.name){
14+
return -1;
15+
}
16+
if(h1.name > h2.name){
17+
return 1
18+
};
19+
return 0;
20+
});
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// #docregion
2+
import {Component} from 'angular2/core';
3+
import {SortedHeroesBase} from './sorted-heroes-base';
4+
import {HeroService} from './hero.service';
5+
6+
@Component({
7+
selector:'sorted-heroes',
8+
template:`<div *ngFor="#hero of sortedHeroes">{{hero.name}}</div>`,
9+
providers:[HeroService]
10+
})
11+
12+
export class SortedHeroes extends SortedHeroesBase{
13+
constructor(heroService:HeroService){
14+
super(heroService);
15+
}
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// #docregion
2+
import {UserService} from './user.service';
3+
import {Injectable} from 'angular2/core';
4+
import {LoggerService} from './logger.service';
5+
6+
@Injectable()
7+
export class UserContext{
8+
9+
name:string;
10+
role:string;
11+
loggedInSince:Date;
12+
13+
constructor(private _userService:UserService, private _loggerService:LoggerService){
14+
this.loggedInSince = new Date();
15+
}
16+
17+
loadUser(userId:number){
18+
let user = this._userService.getUserById(userId);
19+
this.name = user.name;
20+
this.role = user.role;
21+
22+
this._loggerService.logDebug('loaded User');
23+
}
24+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// #docregion
2+
export class UserService{
3+
4+
getUserById(userId:number):any{
5+
return {name:'Bombasto',role:'Admin'};
6+
}
7+
}

public/docs/_examples/cb-dependency-injection/ts/example-config.json

Whitespace-only changes.

0 commit comments

Comments
 (0)