Skip to content

Instantly share code, notes, and snippets.

@latish
Created January 21, 2019 22:25
Show Gist options
  • Save latish/8306fabdf7ec3ec1f1ce2b217e763ceb to your computer and use it in GitHub Desktop.
Save latish/8306fabdf7ec3ec1f1ce2b217e763ceb to your computer and use it in GitHub Desktop.
<app-hero-detail-ui
[hero]="hero$ | async"
(save)="save($event)"
(goBack)="goBack()"
></app-hero-detail-ui>
import { Component, OnInit, Input } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { Hero } from '../hero';
import { HeroService } from '../hero.service';
import { Observable } from 'rxjs';
@Component({
selector: 'app-hero-detail',
templateUrl: './hero-detail.container.component.html',
styleUrls: [ ]
})
export class HeroDetailContainerComponent {
hero$: Observable<Hero>;
constructor(
private route: ActivatedRoute,
private heroService: HeroService,
private location: Location
) {
this.getHero();
}
getHero(): void {
const id = +this.route.snapshot.paramMap.get('id');
this.hero$ = this.heroService.getHero(id);
}
goBack(): void {
this.location.back();
}
save(hero:Hero): void {
this.heroService.updateHero(hero)
.subscribe(() => this.goBack());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment