Created
January 21, 2019 21:42
-
-
Save latish/0e3c55529cd69c292df7c38fcb38c91f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div *ngIf="hero"> | |
<h2>{{hero.name | uppercase}} Details</h2> | |
<div><span>id: </span>{{hero.id}}</div> | |
<div> | |
<label>name: | |
<input [(ngModel)]="hero.name" placeholder="name"/> | |
</label> | |
</div> | |
<button (click)="goBack()">go back</button> | |
<button (click)="save()">save</button> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | |
@Component({ | |
selector: 'app-hero-detail', | |
templateUrl: './hero-detail.component.html', | |
styleUrls: [ './hero-detail.component.css' ] | |
}) | |
export class HeroDetailComponent implements OnInit { | |
@Input() hero: Hero; | |
constructor( | |
private route: ActivatedRoute, | |
private heroService: HeroService, | |
private location: Location | |
) {} | |
ngOnInit(): void { | |
this.getHero(); | |
} | |
getHero(): void { | |
const id = +this.route.snapshot.paramMap.get('id'); | |
this.heroService.getHero(id) | |
.subscribe(hero => this.hero = hero); | |
} | |
goBack(): void { | |
this.location.back(); | |
} | |
save(): void { | |
this.heroService.updateHero(this.hero) | |
.subscribe(() => this.goBack()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment