Skip to content

Commit cde4281

Browse files
committed
resuming learning
31 oct
1 parent cd8f7f1 commit cde4281

File tree

6 files changed

+52
-64
lines changed

6 files changed

+52
-64
lines changed

APM-Start/src/app/app.component.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {Component} from '@angular/core'
2-
2+
import {ProductService} from './products/product.service'
33
@Component({
44
selector:'pm-root',
55
template:`<div>
66
<h1>{{pageTitle}}</h1>
7-
<pm-products></pm-products></div>`
7+
<pm-products></pm-products></div>`,
8+
providers: [ProductService]
89
})
910

1011
export class AppComponent{

APM-Start/src/app/app.module.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { NgModule } from '@angular/core';
33
import { FormsModule} from '@angular/forms'
44
import { AppComponent } from './app.component';
55
import {ProductListComponent} from './products/product-list.component';
6-
import {ProductFilterPipe} from './products/product-filter-pipe'
6+
import {ProductFilterPipe} from './products/product-filter.pipe'
77
import {StarComponent} from './shared/star.component'
8-
8+
import {HttpModule} from '@angular/http'
99
@NgModule({
1010
declarations: [
1111
AppComponent,
@@ -15,7 +15,8 @@ import {StarComponent} from './shared/star.component'
1515
],
1616
imports: [
1717
BrowserModule,
18-
FormsModule
18+
FormsModule,
19+
HttpModule
1920
],
2021
providers: [],
2122
bootstrap: [AppComponent]
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {Component,OnInit} from '@angular/core'
22
import {IProduct} from './product'
3+
import {ProductService} from './product.service'
34
@Component({
45
selector:'pm-products',
56
moduleId:module.id, //use for component relative path
6-
templateUrl:'./product-list.component.html',
7-
styleUrls:['./product-list.component.css']
7+
templateUrl:'product-list.component.html',
8+
styleUrls:['product-list.component.css']
89
})
910

1011
export class ProductListComponent implements OnInit{
@@ -24,67 +25,21 @@ export class ProductListComponent implements OnInit{
2425
imageWidth:number=50;
2526
imageMargin:number=2;
2627
showImage:boolean=false;
27-
listFilter:string='cart';
28-
products:IProduct[]=[
29-
{
30-
"productId": 1,
31-
"productName": "Leaf Rake",
32-
"productCode": "GDN-0011",
33-
"releaseDate": "March 19, 2016",
34-
"description": "Leaf rake with 48-inch wooden handle.",
35-
"price": 19.95,
36-
"starRating": 3.2,
37-
"imageUrl": "http://openclipart.org/image/300px/svg_to_png/26215/Anonymous_Leaf_Rake.png"
38-
},
39-
{
40-
"productId": 2,
41-
"productName": "Garden Cart",
42-
"productCode": "GDN-0023",
43-
"releaseDate": "March 18, 2016",
44-
"description": "15 gallon capacity rolling garden cart",
45-
"price": 32.99,
46-
"starRating": 4.2,
47-
"imageUrl": "http://openclipart.org/image/300px/svg_to_png/58471/garden_cart.png"
48-
},
49-
{
50-
"productId": 5,
51-
"productName": "Hammer",
52-
"productCode": "TBX-0048",
53-
"releaseDate": "May 21, 2016",
54-
"description": "Curved claw steel hammer",
55-
"price": 8.9,
56-
"starRating": 4.8,
57-
"imageUrl": "http://openclipart.org/image/300px/svg_to_png/73/rejon_Hammer.png"
58-
},
59-
{
60-
"productId": 8,
61-
"productName": "Saw",
62-
"productCode": "TBX-0022",
63-
"releaseDate": "May 15, 2016",
64-
"description": "15-inch steel blade hand saw",
65-
"price": 11.55,
66-
"starRating": 3.7,
67-
"imageUrl": "http://openclipart.org/image/300px/svg_to_png/27070/egore911_saw.png"
68-
},
69-
{
70-
"productId": 10,
71-
"productName": "Video Game Controller",
72-
"productCode": "GMG-0042",
73-
"releaseDate": "October 15, 2015",
74-
"description": "Standard two-button video game controller",
75-
"price": 35.95,
76-
"starRating": 4.6,
77-
"imageUrl": "http://openclipart.org/image/300px/svg_to_png/120337/xbox-controller_01.png"
78-
}
79-
];
80-
28+
listFilter:string;
29+
products:IProduct[]=[];
30+
errorMessage:string;
31+
constructor (private _productService: ProductService){
32+
33+
}
8134
toggleImage(): void
8235
{
8336
this.showImage=!this.showImage;
8437
}
8538

8639
ngOnInit():void {
87-
console.log("init")
40+
this._productService.getProducts()
41+
.subscribe(products => this.products=products,
42+
error => this.errorMessage=<any> error);
8843
}
8944

9045

@@ -96,4 +51,6 @@ onRatingClicked(message:string): void {
9651

9752

9853

54+
55+
9956
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Injectable } from '@angular/core'
2+
import { IProduct } from './product';
3+
import { Http, Response} from '@angular/http';
4+
import { Observable } from 'rxjs/Observable';
5+
import 'rxjs/add/operator/map';
6+
import 'rxjs/add/operator/do';
7+
import 'rxjs/add/operator/catch';
8+
9+
@Injectable()
10+
export class ProductService {
11+
12+
private _productUrl="api/products/products.json"
13+
constructor(private _http: Http){
14+
15+
}
16+
getProducts(): Observable<IProduct[]>{
17+
return this._http.get(this._productUrl)
18+
.map((response: Response) => <IProduct[]> response.json())
19+
.do(data => console.log('All'+ JSON.stringify(data)))
20+
.catch(this.handleError);
21+
}
22+
23+
private handleError(error: Response){
24+
console.log(error);
25+
return Observable.throw(error.json().error||"Server error");
26+
}
27+
28+
}

APM-Start/src/app/shared/star.component.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ Output, EventEmitter } from '@angular/core';
33

44
@Component({
55
selector:'ai-star',
6-
templateUrl:'./star.component.html',
7-
styleUrls: ['./star.component.css']
6+
moduleId:module.id,
7+
templateUrl:'star.component.html',
8+
styleUrls: ['star.component.css']
89
})
910
export class StarComponent implements OnChanges{
1011
@Input() rating:number;

0 commit comments

Comments
 (0)