Skip to content

Commit 9a70022

Browse files
committed
As of Module 9
1 parent 76caf1a commit 9a70022

File tree

3 files changed

+29
-36
lines changed

3 files changed

+29
-36
lines changed

APM-CLI-Final/src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BrowserModule } from '@angular/platform-browser';
22
import { NgModule } from '@angular/core';
33
import { FormsModule } from '@angular/forms';
4+
import { HttpModule } from '@angular/http';
45

56
import { AppComponent } from './app.component';
67
import { ProductListComponent } from './products/product-list.component';
@@ -16,7 +17,8 @@ import { StarComponent } from './shared/star.component';
1617
],
1718
imports: [
1819
BrowserModule,
19-
FormsModule
20+
FormsModule,
21+
HttpModule
2022
],
2123
providers: [],
2224
bootstrap: [AppComponent]

APM-CLI-Final/src/app/products/product-list.component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class ProductListComponent implements OnInit {
1313
imageWidth: number = 50;
1414
imageMargin: number = 2;
1515
showImage: boolean = false;
16+
errorMessage: string;
1617

1718
_listFilter: string;
1819
get listFilter(): string {
@@ -45,7 +46,11 @@ export class ProductListComponent implements OnInit {
4546
}
4647

4748
ngOnInit(): void {
48-
this.products = this._productService.getProducts();
49-
this.filteredProducts = this.products;
49+
this._productService.getProducts()
50+
.subscribe(products => {
51+
this.products = products;
52+
this.filteredProducts = this.products;
53+
},
54+
error => this.errorMessage = <any>error);
5055
}
5156
}
Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,28 @@
11
import { Injectable } from '@angular/core';
2+
import { Http, Response } from '@angular/http';
3+
import { Observable } from 'rxjs/Observable';
4+
import 'rxjs/add/observable/throw';
5+
import 'rxjs/add/operator/catch';
6+
import 'rxjs/add/operator/do';
7+
import 'rxjs/add/operator/map';
28

39
import { IProduct } from './product';
410

511
@Injectable()
612
export class ProductService {
13+
private _productUrl = 'api/products/products.json';
714

8-
getProducts(): IProduct[] {
9-
return [
10-
{
11-
"productId": 2,
12-
"productName": "Garden Cart",
13-
"productCode": "GDN-0023",
14-
"releaseDate": "March 18, 2016",
15-
"description": "15 gallon capacity rolling garden cart",
16-
"price": 32.99,
17-
"starRating": 4.2,
18-
"imageUrl": "http://openclipart.org/image/300px/svg_to_png/58471/garden_cart.png"
19-
},
20-
{
21-
"productId": 5,
22-
"productName": "Hammer",
23-
"productCode": "TBX-0048",
24-
"releaseDate": "May 21, 2016",
25-
"description": "Curved claw steel hammer",
26-
"price": 8.9,
27-
"starRating": 4.8,
28-
"imageUrl": "http://openclipart.org/image/300px/svg_to_png/73/rejon_Hammer.png"
29-
},
30-
{
31-
"productId": 10,
32-
"productName": "Video Game Controller",
33-
"productCode": "GMG-0042",
34-
"releaseDate": "October 15, 2015",
35-
"description": "Standard two-button video game controller",
36-
"price": 35.95,
37-
"starRating": 4.6,
38-
"imageUrl": "http://openclipart.org/image/300px/svg_to_png/120337/xbox-controller_01.png"
39-
}
40-
];
15+
constructor(private _http: Http) {}
16+
17+
getProducts(): Observable<IProduct[]> {
18+
return this._http.get(this._productUrl)
19+
.map((response: Response) => <IProduct[]> response.json())
20+
.do(data => console.log('All: ' + JSON.stringify(data)))
21+
.catch(this.handleError);
22+
}
23+
24+
private handleError(error: Response) {
25+
console.error(error);
26+
return Observable.throw(error.json().error || 'Server error');
4127
}
4228
}

0 commit comments

Comments
 (0)