1.7 Angular - Add Navigation With Routing
1.7 Angular - Add Navigation With Routing
mode_edit
Add navigation with routing
Add the ability to navigate between the Heroes and Dashboard views
When users click a hero name in either view, navigate to a detail view of
the selected hero
When users click a deep link in an email, open the detail view for a
particular hero
For the sample application that this page describes, see the live
When you're done, users can navigate the application like this:
https://angular.io/tutorial/tour-of-heroes/toh-pt5 1/23
18/07/2023, 16:28 Angular - Add navigation with routing
PARAMETER DETAILS
https://angular.io/tutorial/tour-of-heroes/toh-pt5 2/23
18/07/2023, 16:28 Angular - Add navigation with routing
src/app/app-routing.module.ts (generated)
Skip to main content
@NgModule({
imports: [
CommonModule
],
declarations: []
})
export class AppRoutingModule { }
src/app/app-routing.module.ts (updated)
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Routes
The next part of the file is where you configure your routes. Routes tell the
Router which view to display when a user clicks a link or pastes a URL into the
browser address bar.
src/app/app-routing.module.ts
PROPERTIES DETAILS
This tells the router to match that URL to path: 'heroes' and display the
HeroesComponent when the URL is something like
localhost:4200/heroes .
RouterModule.forRoot()
The @NgModule metadata initializes the router and starts it listening for
https://angular.io/tutorial/tour-of-heroes/toh-pt5 4/23
18/07/2023, 16:28 Angular - Add navigation with routing
RouterModule.forRoot() :
Skip to main content
src/app/app-routing.module.ts
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
Add RouterOutlet
Open the AppComponent template and replace the <app-heroes> element
with a <router-outlet> element.
src/app/app.component.html (router-outlet)
<h1>{{title}}</h1>
<router-outlet></router-outlet>
<app-messages></app-messages>
https://angular.io/tutorial/tour-of-heroes/toh-pt5 5/23
18/07/2023, 16:28 Angular - Add navigation with routing
Try it
If you're not still serving your application, run ng serve to see your
application in the browser.
The browser should refresh and display the application title but not the list of
heroes.
Look at the browser's address bar. The URL ends in / . The route path to
HeroesComponent is /heroes .
Append /heroes to the URL in the browser address bar. You should see the
familiar heroes overview/detail view.
Remove /heroes from the URL in the browser address bar. The browser
should refresh and display the application title but not the list of heroes.
https://angular.io/tutorial/tour-of-heroes/toh-pt5 6/23
18/07/2023, 16:28 Angular - Add navigation with routing
Add a <nav> element and, within that, an anchor element that, when clicked,
Skip to main content
triggers navigation to the HeroesComponent . The revised AppComponent
template looks like this:
<h1>{{title}}</h1>
<nav>
<a routerLink="/heroes">Heroes</a>
</nav>
<router-outlet></router-outlet>
<app-messages></app-messages>
The browser refreshes and displays the application title and heroes link, but
not the heroes list.
Click the link. The address bar updates to /heroes and the list of heroes
appears.
Make this and future navigation links look better by adding private
CSS styles to app.component.css as listed in the final code
review below.
https://angular.io/tutorial/tour-of-heroes/toh-pt5 7/23
18/07/2023, 16:28 Angular - Add navigation with routing
Skip ng generate
to main component dashboard
content
src/app/dashboard/dashboard.component.html src/app/dashboard/
<h2>Top Heroes</h2>
<div class="heroes-menu">
<a *ngFor="let hero of heroes">
{{hero.name}}
</a>
</div>
https://angular.io/tutorial/tour-of-heroes/toh-pt5 8/23
18/07/2023, 16:28 Angular - Add navigation with routing
src/app/dashboard/dashboard.component.ts
Skip to main content
getHeroes(): void {
this.heroService.getHeroes()
.subscribe(heroes => this.heroes = heroes.slice(1,
5));
}
src/app/app-routing.module.ts
src/app/app-routing.module.ts
https://angular.io/tutorial/tour-of-heroes/toh-pt5 9/23
18/07/2023, 16:28 Angular - Add navigation with routing
This route redirects a URL that fully matches the empty path to the route
Skip to main content
whose path is '/dashboard' .
After the browser refreshes, the router loads the DashboardComponent and
the browser address bar shows the /dashboard URL.
src/app/app.component.html
<h1>{{title}}</h1>
<nav>
<a routerLink="/dashboard">Dashboard</a>
<a routerLink="/heroes">Heroes</a>
</nav>
<router-outlet></router-outlet>
<app-messages></app-messages>
After the browser refreshes you can navigate freely between the two views by
clicking the links.
https://angular.io/tutorial/tour-of-heroes/toh-pt5 10/23
18/07/2023, 16:28 Angular - Add navigation with routing
3. By pasting a "deep link" URL into the browser address bar that identifies
the hero to display.
Clicking a hero item now does nothing. You can fix that after you enable
routing to the HeroDetailComponent .
Then add a parameterized route to the routes array that matches the path
pattern to the hero detail view.
https://angular.io/tutorial/tour-of-heroes/toh-pt5 11/23
18/07/2023, 16:28 Angular - Add navigation with routing
src/app/app-routing.module.ts
Skip to main content
The colon : character in the path indicates that :id is a placeholder for a
specific hero id .
Now that the router has a route to HeroDetailComponent , fix the dashboard
hero links to navigate using the parameterized dashboard route.
https://angular.io/tutorial/tour-of-heroes/toh-pt5 12/23
18/07/2023, 16:28 Angular - Add navigation with routing
<ul class="heroes">
<li *ngFor="let hero of heroes">
<button type="button" (click)="onSelect(hero)"
[class.selected]="hero === selectedHero">
<span class="badge">{{hero.id}}</span>
<span class="name">{{hero.name}}</span>
</button>
</li>
</ul>
Remove the inner HTML of <li> . Wrap the badge and name in an anchor
<a> element. Add a routerLink attribute to the anchor that's the same as
in the dashboard template.
<ul class="heroes">
<li *ngFor="let hero of heroes">
<a routerLink="/detail/{{hero.id}}">
<span class="badge">{{hero.id}}</span> {{hero.name}}
</a>
</li>
</ul>
While the HeroesComponent class still works, the onSelect() method and
selectedHero property are no longer used.
https://angular.io/tutorial/tour-of-heroes/toh-pt5 13/23
18/07/2023, 16:28 Angular - Add navigation with routing
It's nice to tidy things up for your future self. Here's the class after pruning
Skip to main content
away the dead code.
ngOnInit(): void {
this.getHeroes();
}
getHeroes(): void {
this.heroService.getHeroes()
.subscribe(heroes => this.heroes = heroes);
}
}
Routable HeroDetailComponent
The parent HeroesComponent used to set the HeroDetailComponent.hero
property and the HeroDetailComponent displayed the hero.
The HeroDetailComponent needs a new way to get the hero to display. This
https://angular.io/tutorial/tour-of-heroes/toh-pt5 14/23
18/07/2023, 16:28 Angular - Add navigation with routing
src/app/hero-detail/hero-detail.component.ts
Skip to main content
src/app/hero-detail/hero-detail.component.ts
constructor(
private route: ActivatedRoute,
private heroService: HeroService,
private location: Location
) {}
parameters extracted from the URL. The "id" parameter is the id of the hero
to display.
The HeroService gets hero data from the remote server and this
component uses it to get the hero-to-display.
The location is an Angular service for interacting with the browser. This
https://angular.io/tutorial/tour-of-heroes/toh-pt5 15/23
18/07/2023, 16:28 Angular - Add navigation with routing
src/app/hero-detail/hero-detail.component.ts
Skip to main content
ngOnInit(): void {
this.getHero();
}
getHero(): void {
const id =
Number(this.route.snapshot.paramMap.get('id'));
this.heroService.getHero(id)
.subscribe(hero => this.hero = hero);
}
The browser refreshes and the application crashes with a compiler error.
Add HeroService.getHero()
Open HeroService and add the following getHero() method with the id
https://angular.io/tutorial/tour-of-heroes/toh-pt5 16/23
18/07/2023, 16:28 Angular - Add navigation with routing
src/app/hero.service.ts (getHero)
Skip to main content
IMPORTANT:
The backtick ( ` ) characters define a JavaScript template literal
Try it
The browser refreshes and the application is working again. You can click a
hero in the dashboard or in the heroes list and navigate to that hero's detail
view.
https://angular.io/tutorial/tour-of-heroes/toh-pt5 17/23
18/07/2023, 16:28 Angular - Add navigation with routing
It would be nice to have a button on the HeroDetail view that can do that.
Skip to main content
Add a go back button to the bottom of the component template and bind it to
used to inject.
src/app/hero-detail/hero-detail.component.ts (goBack)
goBack(): void {
this.location.back();
}
Refresh the browser and start clicking. Users can now navigate around the
The details look better when you add the private CSS styles to hero-
https://angular.io/tutorial/tour-of-heroes/toh-pt5 18/23
18/07/2023, 16:28 Angular - Add navigation with routing
getHeroes(): Observable<Hero[]> {
const heroes = of(HEROES);
this.messageService.add('HeroService: fetched
heroes');
return heroes;
}
AppComponent
https://angular.io/tutorial/tour-of-heroes/toh-pt5 19/23
18/07/2023, 16:28 Angular - Add navigation with routing
border-radius: 4px;
Skip }to main content
nav a:hover {
color: white;
background-color: #42545C;
}
nav a:active {
background-color: black;
}
DashboardComponent
src/app/dashboard/dashboard.component.ts src/app/dashboard
h2 {
text-align: center;
}
.heroes-menu {
padding: 0;
margin: auto;
max-width: 1000px;
/* flexbox */
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
align-content: flex-start;
align-items: flex-start;
}
a {
background-color: #3f525c;
border-radius: 2px;
padding: 1rem;
font-size: 1.2rem;
text-decoration: none;
display: inline-block;
color: #fff;
text-align: center;
https://angular.io/tutorial/tour-of-heroes/toh-pt5 20/23
18/07/2023, 16:28 Angular - Add navigation with routing
width: 100%;
Skip to min-width:
main content70px;
margin: .5rem auto;
box-sizing: border-box;
/* flexbox */
order: 0;
flex: 0 1 auto;
align-self: auto;
}
a:hover {
background-color: #000;
}
HeroesComponent
.heroes li:hover {
left: .1em;
}
.heroes a {
color: #333;
https://angular.io/tutorial/tour-of-heroes/toh-pt5 21/23
18/07/2023, 16:28 Angular - Add navigation with routing
text-decoration: none;
Skip to background-color:
main content #EEE;
margin: .5em;
padding: .3em 0;
height: 1.6em;
border-radius: 4px;
display: block;
width: 100%;
}
.heroes a:hover {
color: #2c3a41;
background-color: #e6e6e6;
}
.heroes a:active {
background-color: #525252;
color: #fafafa;
}
.heroes .badge {
display: inline-block;
font-size: small;
color: white;
padding: 0.8em 0.7em 0 0.7em;
background-color: #405061;
line-height: 1em;
position: relative;
left: -1px;
top: -4px;
height: 1.8em;
min-width: 16px;
text-align: right;
margin-right: .8em;
border-radius: 4px 0 0 4px;
}
HeroDetailComponent
src/app/hero-detail/hero-detail.component.ts src/app/hero-detai
https://angular.io/tutorial/tour-of-heroes/toh-pt5 22/23
18/07/2023, 16:28 Angular - Add navigation with routing
color: #435960;
font-weight: bold;
}
input {
font-size: 1em;
padding: .5rem;
}
button {
margin-top: 20px;
background-color: #eee;
padding: 1rem;
border-radius: 4px;
font-size: 1rem;
}
button:hover {
background-color: #cfd8dc;
}
button:disabled {
background-color: #eee;
color: #ccc;
cursor: auto;
}
Summary link
You added the Angular router to navigate among different components
You turned the AppComponent into a navigation shell with <a> links
and a <router-outlet>
You used router link parameters to navigate to the detail view of a user-
selected hero
https://angular.io/tutorial/tour-of-heroes/toh-pt5 23/23