File tree Expand file tree Collapse file tree 5 files changed +53
-9
lines changed Expand file tree Collapse file tree 5 files changed +53
-9
lines changed Original file line number Diff line number Diff line change
1
+ //this is how we import external modules
1
2
import { NgModule } from '@angular/core' ;
2
3
import { BrowserModule } from '@angular/platform-browser' ;
4
+ import { FormsModule } from '@angular/forms' ;
3
5
6
+ //this is how we import internal modules
4
7
import { AppComponent } from './app.component' ;
5
8
import { ProductListComponent } from './products/product-list.component' ;
6
9
10
+ //this is how we declare which modules we would like to use from the above imported modules
7
11
@NgModule ( {
8
- imports : [ BrowserModule ] ,
12
+ //imports is used for external/3rd party directives, components and pipes
13
+ imports : [
14
+ BrowserModule ,
15
+ FormsModule
16
+ ] ,
17
+ //declarations is used for our directives, components and pipes
9
18
declarations : [
10
19
AppComponent ,
11
20
ProductListComponent
Original file line number Diff line number Diff line change
1
+ thead {
2
+ color : # 337AB7 ;
3
+ }
Original file line number Diff line number Diff line change 6
6
< div class ="row ">
7
7
< div class ="col-md-2 "> Filtered by:</ div >
8
8
< div class ="col-md-4 ">
9
- < input type ="text " />
9
+ <!-- 2 way data binding i.e. setting the listFilter property in product-list.component.ts to the value of the input -->
10
+ < input type ="text " [(ngModel)] ="listFilter "/>
10
11
</ div >
11
12
</ div >
12
13
< div class ="row ">
13
14
< div class ="col-md-6 ">
14
- < h3 > Filtered by: </ h3 >
15
+ <!-- displaying the value of listFilter which is set by the input above via 2 way data banding -->
16
+ < h3 > Filtered by: {{listFilter}}</ h3 >
15
17
</ div >
16
18
</ div >
17
19
< div class ="table-responsive ">
18
20
< table class ="table " *ngIf ="products && products.length ">
19
21
< thead >
20
22
< tr >
21
23
< th >
22
- < button class ="btn btn-primary "> Show Image</ button >
24
+ < button class ="btn btn-primary "
25
+ (click) ="toggleImage() ">
26
+ {{showImage ? 'Hide' : 'Show'}} Image
27
+ </ button >
23
28
</ th >
24
29
< th > Product</ th >
25
30
< th > Code</ th >
@@ -30,11 +35,17 @@ <h3>Filtered by: </h3>
30
35
</ thead >
31
36
< tbody >
32
37
< tr *ngFor ="let product of products ">
33
- < td > </ td >
38
+ < td >
39
+ < img *ngIf ="showImage "
40
+ [src] ="product.imageUrl "
41
+ [title] ="product.productName "
42
+ [style.width.px] ="imageWidth "
43
+ [style.margin.px] ="imageMargin ">
44
+ </ td >
34
45
< td > {{product.productName}}</ td >
35
- < td > {{product.productCode}}</ td >
46
+ < td > {{product.productCode | lowercase }}</ td >
36
47
< td > {{product.releaseDate}}</ td >
37
- < td > {{product.price}}</ td >
48
+ < td > {{product.price | currency:'USD':true:'1.2-2' }}</ td >
38
49
< td > {{product.starRating}}</ td >
39
50
</ tr >
40
51
</ tbody >
Original file line number Diff line number Diff line change 1
1
import { Component } from '@angular/core' ;
2
+ import { IProduct } from './product' ;
2
3
3
4
@Component ( {
4
5
selector : 'pm-products' ,
5
- templateUrl : 'app/products/product-list.component.html'
6
+ templateUrl : 'app/products/product-list.component.html' ,
7
+ styleUrls : [ 'app/products/product-list.component.css' ]
6
8
} )
7
9
export class ProductListComponent {
8
10
pageTitle : string = 'Product List' ;
9
- products : any [ ] = [
11
+ imageWidth : number = 50 ;
12
+ imageMargin : number = 2 ;
13
+ showImage : boolean = false ;
14
+ listFilter : string = 'cart' ;
15
+
16
+ products : IProduct [ ] = [
10
17
{
11
18
"productId" : 1 ,
12
19
"productName" : "Leaf Rake" ,
@@ -58,4 +65,8 @@ export class ProductListComponent {
58
65
"imageUrl" : "http://openclipart.org/image/300px/svg_to_png/120337/xbox-controller_01.png"
59
66
}
60
67
] ;
68
+
69
+ toggleImage ( ) : void {
70
+ this . showImage = ! this . showImage ;
71
+ }
61
72
}
Original file line number Diff line number Diff line change
1
+ export interface IProduct {
2
+ productId : number ,
3
+ productName : string ,
4
+ productCode : string ,
5
+ releaseDate : string ,
6
+ description : string ,
7
+ price : number ,
8
+ starRating : number ,
9
+ imageUrl : string
10
+ }
You can’t perform that action at this time.
0 commit comments