-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhero-detail.component.ts
53 lines (44 loc) · 1.34 KB
/
hero-detail.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { Hero } from '../hero';
import { HeroService } from '../hero.service';
@Component({
selector: 'app-hero-detail',
templateUrl: './hero-detail.component.html',
styleUrls: [ './hero-detail.component.css' ]
})
export class HeroDetailComponent implements OnInit {
hero: Hero = { id: 0, name: "" };
@ViewChild("editor") editor!: ElementRef;
constructor(
private route: ActivatedRoute,
private heroService: HeroService,
private location: Location
) {}
ngOnInit(): void {
this.getHero();
}
ngAfterViewInit(): void {
// Register web component event (we need to bind "this" to the Angular component!)
this.editor.nativeElement.heroChanged = this.onHeroChanged.bind(this);
}
getHero(): void {
const id = parseInt(this.route.snapshot.paramMap.get('id')!, 10);
this.heroService.getHero(id)
.subscribe(hero => this.hero = hero);
}
onHeroChanged(hero: Hero) {
console.log("Hero changed from Blazor component: " + hero.name);
this.hero = hero;
}
goBack(): void {
this.location.back();
}
save(): void {
if (this.hero) {
this.heroService.updateHero(this.hero)
.subscribe(() => this.goBack());
}
}
}