File

src/app/hero-detail/hero-detail.component.ts

Description

Component for the management of details of the Heroes.

Implements

OnInit

Metadata

selector app-hero-detail
styleUrls ./hero-detail.component.css
templateUrl ./hero-detail.component.html

Index

Methods
Inputs

Constructor

constructor(route: ActivatedRoute, heroService: HeroService, location: Location)
Parameters :
Name Type Optional
route ActivatedRoute No
heroService HeroService No
location Location No

Inputs

hero

Hero selected

Type : Hero

Methods

getHero
getHero()

Loads specific Hero based on URL id

Returns : void
goBack
goBack()

Go back last web page

Returns : void
ngOnInit
ngOnInit()

When webpage is loaded, gets hero id from the URL

Returns : void
save
save()

Uploads specific Hero data based on URL id

Returns : void
import { Component, OnInit, Input } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';

import { Hero }         from '../hero';
import { HeroService }  from '../providers/hero.service';

/** Component for the management of details of the Heroes. */
@Component({
  selector: 'app-hero-detail',
  templateUrl: './hero-detail.component.html',
  styleUrls: [ './hero-detail.component.css' ]
})
export class HeroDetailComponent implements OnInit {

  /** Hero selected */
  @Input() hero: Hero;

  constructor(
    private route: ActivatedRoute,
    private heroService: HeroService,
    private location: Location
  ) {}

  /** When webpage is loaded, gets hero id from the URL */
  ngOnInit(): void {
    this.getHero();
  }

  /** Loads specific Hero based on URL id */
  getHero(): void {
    const id = +this.route.snapshot.paramMap.get('id');
    this.heroService.getHero(id)
      .subscribe(hero => this.hero = hero);
  }

  /** Go back last web page */
  goBack(): void {
    this.location.back();
  }

  /** Uploads specific Hero data based on URL id */
 save(): void {
    this.heroService.updateHero(this.hero)
      .subscribe(() => this.goBack());
  }
}
<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>

./hero-detail.component.css

/* HeroDetailComponent's private CSS styles */
label {
  display: inline-block;
  width: 3em;
  margin: .5em 0;
  color: #607D8B;
  font-weight: bold;
}
input {
  height: 2em;
  font-size: 1em;
  padding-left: .4em;
}
button {
  margin-top: 20px;
  font-family: Arial;
  background-color: #eee;
  border: none;
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer; cursor: hand;
}
button:hover {
  background-color: #cfd8dc;
}
button:disabled {
  background-color: #eee;
  color: #ccc;
  cursor: auto;
}
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""