ngIf is a structural directive that removes or recreates a portion of the dom tree based on an expression. If the expression assigned to the ngIf evaluates to a false value, the element and its children are removed from the dom. If the expression evaluates to a true value, a copy of the element and its children are reinserted into the dom.
For example, say if one wanted to show the HTML table and there are some products in a list of products. Use ngIf on the table element and set it to “products && products.length”. If the products variable has a value and the products list has length, the table appears in the dom. If not, the table element and all of its children are removed from the dom.
So, first define a product property in the components class, and just define the product as an array of any as shown in the below code.
product-list.component.ts
[c]
import { Component } from '@angular/core';
@Component({
selector: 'pm-products',
templateUrl: 'app/products/product-list.component.html'
})
export class ProductListComponent {
products: any[];
}
[/c]
In TypeScript, any is used as the data type if one don’t know or don’t care what the specific data type is. In many case one will communicate with a back end server to get the data but as of now just enter few product details as shown in the code below.
product-list.component.ts
[c]
import { Component } from '@angular/core';
@Component({
selector: 'pm-products',
templateUrl: 'app/products/product-list.component.html'
})
export class ProductListComponent {
products: any[] = [
{
"productId": 1,
"productName": "Angular 2",
"productCode": "GDN-0011",
"releaseDate": "March 19, 2016",
"description": "A Practical Introduction to the new Web Development Platform Angular 2",
"price": 19.95,
"starRating": 3.2
},
{
"productId": 2,
"productName": "Word Press",
"productCode": "GDN-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
}
];
}
[/c]
Now after placing the product property one can use it in HTML by putting in the on the table element so that it can be added or removed from the dom. Type *ngIf equals and then the expression enclose the “products && products.lenght” in single quotes as shown in the code below.
product-list.component.html
[html]
<div class='panel panel-primary'>
<div class='panel-heading'>
Product List
</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'>
Show Image
</button>
</th>
<th>Product</th>
<th>Code</th>
<th>Available</th>
<th>Price</th>
<th>5 Star Rating</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
[/html]
Check the output in browser, one can see the table header, so the table is displayed as shown in the image below.
Now, just comment out the product property assignment in the
product-list.component.ts file as shown in the below code.
product-list.component.ts
[c]
import { Component } from '@angular/core';
@Component({
selector: 'pm-products',
templateUrl: 'app/products/product-list.component.html'
})
export class ProductListComponent {
// 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
// },
// {
// "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
// }
// ];
}
[/c]
Check the output in the browser, one can notice that the table get disappeared.