import { RouterModule, Routes } from '@angular/router';
import { HeroesComponent } from './heroes/heroes.component';
import { DashboardComponent } from './dashboard/dashboard.component';
+import { HeroDetailComponent} from './hero-detail/hero-detail.component';
const routes : Routes = [
{ path: '', redirectTo: 'dashboard', pathMatch: 'full'},
{ path: 'heroes', component: HeroesComponent },
+ { path: 'detail/:id', component: HeroDetailComponent },
{ path: 'dashboard', component: DashboardComponent }
];
<h3>Top Heroes</h3>
<div class="grid grid-pad">
- <a *ngFor="let hero of heroes" class="col-1-4">
+ <a *ngFor="let hero of heroes" class="col-1-4" routerLink="/detail/{{hero.id}}">
<div class="module hero">
<h4>{{hero.name}}</h4>
</div>
width: 15em;
}
.heroes li {
- cursor: pointer;
position: relative;
- left: 0;
+ cursor: pointer;
background-color: #EEE;
margin: .5em;
padding: .3em 0;
height: 1.6em;
border-radius: 4px;
}
+
.heroes li:hover {
color: #607D8B;
background-color: #DDD;
left: .1em;
}
-.heroes li.selected {
- background-color: #CFD8DC;
- color: white;
+
+.heroes a {
+ color: #333;
+ text-decoration: none;
+ position: relative;
+ display: block;
+ width: 250px;
}
-.heroes li.selected:hover {
- background-color: #BBD8DC;
- color: white;
+
+.heroes a:hover {
+ color:#607D8B;
}
+
.heroes .badge {
display: inline-block;
font-size: small;
left: -1px;
top: -4px;
height: 1.8em;
+ min-width: 16px;
+ text-align: right;
margin-right: .8em;
border-radius: 4px 0 0 4px;
}
<h2>My Heroes</h2>
<ul class="heroes">
- <li *ngFor="let hero of heroes" (click)="onSelect(hero)" [class.selected]="hero === selectedHero">
- <span class="badge">{{hero.id}}</span> {{hero.name}}
+ <li *ngFor="let hero of heroes">
+ <a routerLink="/detail/{{hero.id}}">
+ <span class="badge">{{hero.id}}</span> {{hero.name}}
+ </a>
</li>
</ul>
-<app-hero-detail [hero]="selectedHero"></app-hero-detail>
heroes: Hero[];
- selectedHero : Hero;
-
- onSelect(hero : Hero): void {
- this.selectedHero = hero;
- this.messageService.add(`HeroService: Selected hero id=${hero.id}`);
- }
-
constructor(
private heroService : HeroService,
private messageService : MessageService) { }