1
0
mirror of https://github.com/Microsoft/sql-server-samples.git synced 2025-12-08 14:58:54 +00:00
Files
sql-server-samples/samples/features/json/angularjs/dotnet-tour-of-heroes/wwwroot/app/heroes.component.ts
Jovan Popovic 08cce021ee AngularJS Hero app
Working version of Hour of heroes app.
2016-10-29 09:34:49 -07:00

64 lines
1.4 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Hero } from './hero';
import { HeroService } from './hero.service';
@Component({
moduleId: module.id,
selector: 'my-heroes',
templateUrl: 'heroes.component.html',
styleUrls: ['heroes.component.css']
})
export class HeroesComponent implements OnInit {
heroes: Hero[];
selectedHero: Hero;
addingHero = false;
error: any;
constructor(
private router: Router,
private heroService: HeroService) { }
getHeroes(): void {
this.heroService
.getHeroes()
.then(heroes => this.heroes = heroes)
.catch(error => this.error = error);
}
addHero(): void {
this.addingHero = true;
this.selectedHero = null;
}
close(savedHero: Hero): void {
this.addingHero = false;
if (savedHero) { this.getHeroes(); }
}
deleteHero(hero: Hero, event: any): void {
event.stopPropagation();
this.heroService
.delete(hero)
.then(res => {
this.heroes = this.heroes.filter(h => h !== hero);
if (this.selectedHero === hero) { this.selectedHero = null; }
})
.catch(error => this.error = error);
}
ngOnInit(): void {
this.getHeroes();
}
onSelect(hero: Hero): void {
this.selectedHero = hero;
this.addingHero = false;
}
gotoDetail(): void {
this.router.navigate(['/detail', this.selectedHero.id]);
}
}