Skip to content

Commit 044c203

Browse files
committed
progress
section 11
1 parent cde4281 commit 044c203

8 files changed

+98
-9
lines changed

Diff for: APM-Start/src/app/app.component.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,21 @@ import {Component} from '@angular/core'
22
import {ProductService} from './products/product.service'
33
@Component({
44
selector:'pm-root',
5-
template:`<div>
6-
<h1>{{pageTitle}}</h1>
7-
<pm-products></pm-products></div>`,
5+
template:`
6+
<div>
7+
<nav class='navbar navbar-default'>
8+
<div class='container-fluid'>
9+
<a class='navbar-brand'>{{pageTite}}</a>
10+
<ul class='nav navbar-nav'>
11+
<li><a [routerLink]="['/welcome']">Home</a></li>
12+
<li><a [routerLink]="['/products']">Product List</a></li>
13+
</ul>
14+
</div>
15+
</nav>
16+
<div class='container'>
17+
<router-outlet></router-outlet>
18+
</div>
19+
</div>`,
820
providers: [ProductService]
921
})
1022

Diff for: APM-Start/src/app/app.module.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,36 @@ import {ProductListComponent} from './products/product-list.component';
66
import {ProductFilterPipe} from './products/product-filter.pipe'
77
import {StarComponent} from './shared/star.component'
88
import {HttpModule} from '@angular/http'
9+
import { WelcomeComponent } from './home/welcome.component'
10+
import { ProductDetailComponent } from './products/product-detail.component'
11+
12+
import { RouterModule } from '@angular/router'
13+
14+
import { ProductDetailGuard } from'./products/product-guard.service';
915
@NgModule({
1016
declarations: [
1117
AppComponent,
1218
ProductListComponent,
1319
ProductFilterPipe,
14-
StarComponent
20+
StarComponent,
21+
ProductDetailComponent,
22+
WelcomeComponent
1523
],
1624
imports: [
1725
BrowserModule,
1826
FormsModule,
19-
HttpModule
27+
HttpModule,
28+
RouterModule.forRoot([
29+
{ path: 'products', component: ProductListComponent},
30+
{ path: 'product/:id',
31+
canActivate:[ProductDetailGuard], component: ProductDetailComponent},
32+
{ path: 'welcome', component: WelcomeComponent},
33+
{ path: ' ', redirectTo:'welcome', pathMatch:'full'},
34+
{ path: '**', redirectTo:'welcome', pathMatch:'full' }
35+
36+
])
2037
],
21-
providers: [],
38+
providers: [ProductDetailGuard],
2239
bootstrap: [AppComponent]
2340
})
2441

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div class='panel panel-primary'>
2+
<div class="panel-heading">
3+
{{pageTitle}}
4+
</div>
5+
<div class="panel-footer">
6+
<a class="btn btn-default" (click)='onBack()' style='width:80px'>
7+
<i class="glyphicon glyphicon-chevron-left"></i> Back
8+
</a>
9+
</div>
10+
</div>
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {Component,OnInit} from '@angular/core'
2+
import {IProduct} from './product'
3+
4+
import { ActivatedRoute, Router } from '@angular/router'
5+
@Component({
6+
templateUrl:'product-detail.component.html'
7+
})
8+
export class ProductDetailComponent implements OnInit{
9+
pageTitle:string = 'Product Detail';
10+
product: IProduct;
11+
12+
constructor(private _route:ActivatedRoute,
13+
private _router: Router)
14+
{
15+
16+
}
17+
18+
ngOnInit():void {
19+
let id=+this._route.snapshot.params['id']; // + to convert to number
20+
this.pageTitle+=`: ${id}`;
21+
}
22+
23+
onBack():void {
24+
this._router.navigate(['/products']);
25+
}
26+
}

Diff for: APM-Start/src/app/products/product-guard.service.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Injectable } from '@angular/core';
2+
import { CanActivate, ActivatedRouteSnapshot, Router } from '@angular/router';
3+
4+
@Injectable()
5+
export class ProductDetailGuard implements CanActivate {
6+
7+
constructor(
8+
private _router: Router){
9+
10+
}
11+
canActivate(route: ActivatedRouteSnapshot): boolean {
12+
13+
let id = +route.url[1].path;
14+
if(isNaN(id) || id <1 )
15+
{
16+
alert('Invalid product id');
17+
18+
this._router.navigate(['/products']);
19+
return false;
20+
}
21+
22+
return true;
23+
}
24+
}

Diff for: APM-Start/src/app/products/product-list.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ <h3>Filtered by: {{listFilter}}</h3>
3737
<td><img *ngIf="showImage" [src]='product.imageUrl' [title]='product.productName'
3838
[style.width.px]='imageWidth'
3939
[style.margin.px]='imageMargin'></td> <!-- property binding -->
40-
<td>{{product.productName}}</td>
40+
<td><a [routerLink]="['/product',product.productId]">{{product.productName}}</a></td>
4141
<td>{{product.productCode}}</td>
4242
<td>{{product.releaseDate}}</td>
4343
<td>{{product.price | currency:'USD':true:'1.2-2' | lowercase}}</td>

Diff for: APM-Start/src/app/products/product-list.component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Component,OnInit} from '@angular/core'
22
import {IProduct} from './product'
33
import {ProductService} from './product.service'
44
@Component({
5-
selector:'pm-products',
5+
//selector:'pm-products',
66
moduleId:module.id, //use for component relative path
77
templateUrl:'product-list.component.html',
88
styleUrls:['product-list.component.css']

Diff for: APM-Start/src/app/products/product.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class ProductService {
1616
getProducts(): Observable<IProduct[]>{
1717
return this._http.get(this._productUrl)
1818
.map((response: Response) => <IProduct[]> response.json())
19-
.do(data => console.log('All'+ JSON.stringify(data)))
19+
.do(data => console.log('All'+ JSON.stringify(data))) // to peak into response data
2020
.catch(this.handleError);
2121
}
2222

0 commit comments

Comments
 (0)