Skip to content

Instantly share code, notes, and snippets.

@nishio-dens
Created October 25, 2015 10:49
Show Gist options
  • Save nishio-dens/89700798bd85d93a9a36 to your computer and use it in GitHub Desktop.
Save nishio-dens/89700798bd85d93a9a36 to your computer and use it in GitHub Desktop.
AppComponentからServiceにアクセスするにはforwardRefを使う
import {bootstrap, Component, View, FORM_DIRECTIVES, CORE_DIRECTIVES, Inject, forwardRef} from 'angular2/angular2';
class Hero {
id: number;
name: string;
birthday: Date;
}
@Component({
selector: 'hero-app'
})
@View({
templateUrl: 'app/app.html',
styles: [`
.heroes {list-style-type: none; margin-left: 1em; padding: 0; width: 10em;}
.heroes li { cursor: pointer; position: relative; left: 0; transition: all 0.2s ease; }
.heroes li:hover {color: #369; background-color: #EEE; left: .2em;}
.heroes .badge {
font-size: small;
color: white;
padding: 0.1em 0.7em;
background-color: #369;
line-height: 1em;
position: relative;
left: -1px;
top: -1px;
}
.selected { background-color: #EEE; color: #369; }
`],
directives: [FORM_DIRECTIVES, CORE_DIRECTIVES]
})
class AppComponent {
public title = 'Tour of Heroes';
public heroes: Hero[];
public selectedHero: Hero;
constructor(@Inject(forwardRef(() => HeroService)) service) {
this.heroes = service.getHeroes();
}
onSelect(hero: Hero) { this.selectedHero = hero; }
getSelectedClass(hero: Hero) {
return {
'selected': hero === this.selectedHero
};
}
}
class HeroService {
public heroes: Hero[];
constructor() {
this.heroes = [
{ id: 1, name: 'Mr nice', birthday: new Date(2015, 1, 1) },
{ id: 2, name: 'Mr nishio', birthday: new Date(2015, 1, 2) },
{ id: 3, name: 'Yamada', birthday: new Date(2015, 1, 2) }
];
}
getHeroes() {
return this.heroes;
}
}
bootstrap(AppComponent, [HeroService]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment