Skip to content

Commit efee47f

Browse files
committed
Module 12: Navigation and Routing Additional Techniques.
1 parent 3d9c518 commit efee47f

File tree

6 files changed

+64
-28
lines changed

6 files changed

+64
-28
lines changed

APM/src/app/app.module.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { ConvertToSpacesPipe } from './shared/convert-to-spaces.pipe';
1010
import { StarComponent } from './shared/star.component';
1111
import { ProductDetailComponent } from './products/product-detail.component';
1212
import { WelcomeComponent } from './home/welcome.component';
13+
import { ProductDetailGuard } from './products/product-detail.guard';
1314

1415
@NgModule({
1516
declarations: [
@@ -26,7 +27,11 @@ import { WelcomeComponent } from './home/welcome.component';
2627
HttpClientModule,
2728
RouterModule.forRoot([
2829
{ path: 'products', component: ProductListComponent },
29-
{ path: 'products/:id', component: ProductDetailComponent },
30+
{
31+
path: 'products/:id',
32+
canActivate: [ProductDetailGuard],
33+
component: ProductDetailComponent
34+
},
3035
{ path: 'welcome', component: WelcomeComponent },
3136
{ path: '', redirectTo: 'welcome', pathMatch: 'full' },
3237
{ path: '**', redirectTo: 'welcome', pathMatch: 'full'},

APM/src/app/products/product-detail.component.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@
22
<div class="card-header">
33
{{pageTitle + ': ' + product.productName}}
44
</div>
5+
<div>
6+
<button class="btn btn-outline-secondary"
7+
(click)="onBack()"
8+
style="width:80px">
9+
<i class="fa fa-chevron-left"></i> Back
10+
</button>
11+
</div>
512
</div>

APM/src/app/products/product-detail.component.spec.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { ActivatedRoute, Router } from '@angular/router';
3+
4+
import { IProduct } from './products';
25

36
@Component({
47
templateUrl: './product-detail.component.html',
58
styleUrls: ['./product-detail.component.css']
69
})
710
export class ProductDetailComponent implements OnInit {
11+
pageTitle: string = 'Product Detail';
12+
product: IProduct;
813

9-
constructor() { }
14+
constructor(private route: ActivatedRoute,
15+
private router: Router) { }
1016

1117
ngOnInit() {
18+
let id = +this.route.snapshot.paramMap.get('id');
19+
this.pageTitle += `: ${id}`;
20+
this.product = {
21+
"productId": id,
22+
"productName": "Leaf Rake",
23+
"productCode": "GDN-0011",
24+
"releaseDate": "March 19, 2019",
25+
"description": "Leaf rake with 48-inch wooden handle.",
26+
"price": 19.95,
27+
"starRating": 3.2,
28+
"imageUrl": "assets/images/leaf_rake.png"
29+
}
30+
}
31+
32+
onBack(): void {
33+
this.router.navigate(['/products']);
1234
}
1335

1436
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Injectable } from '@angular/core';
2+
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
3+
import { Observable } from 'rxjs';
4+
5+
@Injectable({
6+
providedIn: 'root'
7+
})
8+
export class ProductDetailGuard implements CanActivate {
9+
constructor(private router: Router) {}
10+
11+
canActivate(
12+
next: ActivatedRouteSnapshot,
13+
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
14+
let id = +next.url[1].path;
15+
if (isNaN(id) || id < 1 ) {
16+
alert("Invalide product Id");
17+
this.router.navigate(['/products']);
18+
return false;
19+
};
20+
return true;
21+
}
22+
23+
}

APM/src/app/products/product-list.component.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ <h4>Filtered by: {{listFilter}} </h4>
4242
[style.width.px]='imageWidth'
4343
[style.margin.px]='imageMargin'>
4444
</td>
45-
<td>{{ product.productName }}</td>
45+
<td>
46+
<a [routerLink]="['/products', product.productId]">
47+
{{ product.productName }}
48+
</a>
49+
</td>
4650
<td>{{ product.productCode | lowercase | convertToSpaces: '-' }}</td>
4751
<td>{{ product.releaseDate }}</td>
4852
<td>{{ product.price | currency:'USD':'symbol':'1.2-2' }}</td>

0 commit comments

Comments
 (0)