In order to implement the
Show image button in the demo project product management, first define a class property that keeps track of whether the images are currently displayed. The property can be called as
show image, since the property is true or false, define the property type as
Boolean and set the default value to
false. So, the images are not displayed when the page is first loaded
Next, build the method that the event binding will call. By convention the methods are defined after all of the properties are defined, so put the method down of all properties. Assume the method name as
toggle image and notice that typescript does not require any keyword like function, following with the method name with open and closing parentheses will identifies it as a method, the method won’t have a return type so just specify the return type as
void and the body of the method simply toggles the state of the show image property.
The code below contain the defined class property
showImage and its method
toggleImage at the bottom of all properties.
product-list.component.ts
[c]
import { Component } from '@angular/core';
@Component({
selector: 'pm-products',
templateUrl: 'app/products/product-list.component.html'
})
export class ProductListComponent {
pageTitle: string = 'Product List!';
imageWidth: number = 50;
imageMargin: number = 2;
showImage: boolean = false;
products: any[] = [
{
"productId": 1,
"productName": "Angular 2",
"productCode": "SPL-0011",
"releaseDate": "March 19, 2016",
"description": "A Practical Introduction to the new Web Development Platform Angular 2",
"price": 19.95,
"starRating": 3.2,
"imageUrl": "./app/assets/images/angular_2.png"
},
{
"productId": 2,
"productName": "WordPress",
"productCode": "SPL-0023",
"releaseDate": "March 18, 2016",
"description": "This ebook allows you to create a WordPress website on your domainfrom scratch.",
"price": 32.99,
"starRating": 4.2,
"imageUrl": "./app/assets/images/wordpress-logo.png"
}
];
toggleImage(): void{
this.showImage = !this.showImage;
}
}
[/c]
Now, setup the event binding in the template, open the associate template file and on the button class element define the
click as target event by enclosing it in parentheses and assign it to the created method i.e.
toggleImage enclosed in quotes. So, when the user click on the button the binding calls the method.
Using interpolation the button text can be changed to hide image when the show image is true and when the show image is false the button text display the show image and this can be done using
JavaScript conditional operator as shown in the syntax.
[c]{ { showImage ? ‘Hide’ : ‘Show’} } Image[/c]
Basically the above syntax can be read as, if show image is
true display
hide otherwise display
show. Place the above syntax in the button class element as shown in the code below.
[html]
<div class='panel panel-primary'>
<div class='panel-heading'>
{ {pageTitle} }
</div>
<div class='panel-body'>
<div class='row'>
<div class='col-md-2'>Filter by:</div>
<div class='col-md-4'>
<input type='text' />
</div>
</div>
<div class ='row'>
<div class='col-md-6'>
<h3>Filtered by: </h3>
</div>
</div>
<div class='table-responsive'>
<table class='table'
*ngIf='products && products.length'>
<thead>
<tr>
<th>
<button class='btn btn-success'
(click)='toggleImage()'>
{ {showImage ? 'Hide' : 'Show'} } Image
</button>
</th>
<th>Product</th>
<th>Code</th>
<th>Available</th>
<th>Price</th>
<th>5 Star Rating</th>
</tr>
</thead>
<tbody>
<tr *ngFor='let product of products'>
<td>
<img *ngIf='showImage'
[src]='product.imageUrl'
='product.productName'
[style.width.px]='imageWidth'
[style.margin.px]='imageMargin' />
</td>
<td>{ { product.productName } }</td>
<td>{ { product.productCode } }</td>
<td>{ { product.releaseDate } }</td>
<td>{ { product.price } }</td>
<td>{ { product.starRating } }</td>
</tr>
</tbody>
</table>
</div>
</div>
[/html]
Next in order to hide or show the image add a logic called ngIf directive to the image element. The ngIf directive is used to add or remove the HTML elements from the DOM.