-
New Project
ng new workshop
-
Change directories
cd workshop
-
Start the server
ng serve
-
Open a browser to http://localhost:4200
-
Open another Terminal in the
workshop
directory -
New "Home" Route
ng g r home
-
Add Navigation in
workshop.component.html
<a [routerLink]="['/home']">Home</a>
-
Add Two More Routes
ng g r team ng g r detail
-
Add an
id
param to the detail path inwalkthrough.component.ts
{path: '/detail/:id', component: DetailComponent}
-
Add Navigation to Those Routes
<a [routerLink]="['/team']">Team</a> <a [routerLink]="['/detail', 1]">Detail</a>
-
Generate a new service
ng g s shared/team
-
Add a
name
property to the serviceimport { Injectable } from '@angular/core'; @Injectable() export class TeamService { name = "Bulls"; constructor() {} }
-
Import and Provide the service to
workshop.component.ts
import { TeamService } from './shared' providers: [ROUTER_PROVIDERS, TeamService]
-
Import and Inject the service into
home.component.ts
import { TeamService } from '../shared'; constructor(public team:TeamService) {}
-
Access a Property on the Service in
home.component.html
<p> {{team.name}} </p>
-
Import and Inject the Service into
team.component.ts
import { TeamService } from '../shared'; constructor(public team:TeamService) {}
-
Update the Team
name
onteam.component.html
<input type="text" [(ngModel)]="team.name">
-
Generate a new Component
ng g c shared/card
-
Import the Component and add to
team.component.ts
import { TeamService, Card } from '../shared';
-
Add the
CardComponent
to theteam.component.ts
'sdirectives:[]
directives:[CardComponent],
-
Add the component to the
team.component.html
template<app-card></app-card>
-
Add style to the component with
:host
:host{ display: flex; border: 2px solid black; }
-
Move the input inside the card component in
team.component.html
<app-card> <input type="text" [(ngModel)]="team.name"> </app-card>
-
Handle content with
<ng-content>
incard.component.html
<ng-content></ng-content>
-
Add an array of people to the
team.service.ts
import { Injectable } from '@angular/core'; @Injectable() export class TeamService { name = "Bulls"; people = [ {name: 'Susan', id: Math.random()}, {name: 'Anne', id: Math.random()}, {name: 'John', id: Math.random()}, {name: 'Mindy', id: Math.random()}, {name: 'Ben', id: Math.random()}, {name: 'Jordan', id: Math.random()}, {name: 'Mike', id: Math.random()} ]; constructor() {} }
-
Create Multiple Components using
*ngFor
onteam.component.html
.<app-card *ngFor="let person of team.people"> <input type="text" [(ngModel)]="person.name"> </app-card>
-
Add the
ROUTER_DIRECTIVES
toteam.component.ts
import {ROUTER_DIRECTIVES} from '@angular/router'; directives:[CardComponent, ROUTER_DIRECTIVES],
-
Add a
[routerLink]
to each component and link to theperson.id
<app-card *ngFor="let person of team.people"> <input type="text" [(ngModel)]="person.name"> <a [routerLink]="['/detail', person.id]">Details</a> </app-card>
-
Add the
OnActivate
LifeCycle hook todetail.component.ts
import { Component, OnInit } from '@angular/core'; import {OnActivate, RouteSegment} from '@angular/router'; import {TeamService} from '../shared'; @Component({ moduleId: module.id, selector: 'app-detail', templateUrl: 'detail.component.html', styleUrls: ['detail.component.css'] }) export class DetailComponent implements OnInit, OnActivate { routerOnActivate(curr:RouteSegment):void { } constructor(public team:TeamService) {} ngOnInit() { } }
-
Find the person by looking up the
id
in thepeople
person; routerOnActivate(curr:RouteSegment):void { const id = curr.getParam('id'); this.person = this.team.people.find(person => person.id === +id ); }
-
Render out the
person
indetail.component.html
<h2>{{person.name}}</h2> <h3>{{person.id}}</h3>