|
| 1 | + |
| 2 | +// Import the core angular services. |
| 3 | +import { ActivatedRoute } from "@angular/router"; |
| 4 | +import { Component } from "@angular/core"; |
| 5 | + |
| 6 | +// Import the application components and services. |
| 7 | +import { PeopleService } from "./people.service"; |
| 8 | +import { Person } from "./people.service"; |
| 9 | + |
| 10 | +// ----------------------------------------------------------------------------------- // |
| 11 | +// ----------------------------------------------------------------------------------- // |
| 12 | + |
| 13 | +@Component({ |
| 14 | + selector: "my-people-detail", |
| 15 | + styleUrls: [ "./people-detail.component.less" ], |
| 16 | + template: |
| 17 | + ` |
| 18 | + <h2> |
| 19 | + People Detail |
| 20 | + </h2> |
| 21 | +
|
| 22 | + <div *ngIf="person"> |
| 23 | +
|
| 24 | + <dl class="details"> |
| 25 | + <dt class="details__label">ID</dt> |
| 26 | + <dd class="details__value">{{ person.id }}</dd> |
| 27 | +
|
| 28 | + <dt class="details__label">Name</dt> |
| 29 | + <dd class="details__value">{{ person.name }}</dd> |
| 30 | +
|
| 31 | + <dt class="details__label">Email</dt> |
| 32 | + <dd class="details__value">{{ person.email }}</dd> |
| 33 | + </dl> |
| 34 | +
|
| 35 | + </div> |
| 36 | + ` |
| 37 | +}) |
| 38 | +export class PeopleDetailComponent { |
| 39 | + |
| 40 | + public person: Person | null; |
| 41 | + |
| 42 | + private activatedRoute: ActivatedRoute; |
| 43 | + private peopleService: PeopleService; |
| 44 | + |
| 45 | + // I initialize the people-detail view component. |
| 46 | + constructor( |
| 47 | + activatedRoute: ActivatedRoute, |
| 48 | + peopleService: PeopleService |
| 49 | + ) { |
| 50 | + |
| 51 | + this.activatedRoute = activatedRoute; |
| 52 | + this.peopleService = peopleService; |
| 53 | + |
| 54 | + this.person = null; |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + // --- |
| 59 | + // PUBLIC METHODS. |
| 60 | + // --- |
| 61 | + |
| 62 | + // I get called once after the inputs have been bound for the first time. |
| 63 | + public ngOnInit() : void { |
| 64 | + |
| 65 | + var id = this.activatedRoute.snapshot.params.personID; |
| 66 | + |
| 67 | + this.peopleService.getPerson( id ).then( |
| 68 | + ( person ) => { |
| 69 | + |
| 70 | + this.person = person; |
| 71 | + // In order to make it more clear how the "replaceUrl" in the search view |
| 72 | + // is affecting the browser's history, let's all update the document |
| 73 | + // title - this value will show up in the back-button drop-down menu. |
| 74 | + document.title = `Person: ${ person.name }`; |
| 75 | + |
| 76 | + }, |
| 77 | + ( error ) => { |
| 78 | + |
| 79 | + console.warn( "Oh noes!" ); |
| 80 | + console.error( error ); |
| 81 | + |
| 82 | + } |
| 83 | + ); |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments