I am taking one real time example which displays a list of available products based on the product selection. In this example, you will learn -
  1. How to create a Products array object using TypeScript in a class;
  2. Bind Products to dropdown list with *ngFor;
  3. Call a method to search a list of products using "ngModelChange" ;
  4. Bind Product details object to an HTML table.
Now, let's start entering the code step by step.
Build an array object and store some data in it using TypeScript in a component class.
  1. export class ProductComponent {
  2. /// this data used to display in products dropdown
  3. public ProductHeader = [{ name: 'Hp' }, { name: 'Dell'}, { name: 'Lenovo' }];
  4. /// this data used to display in products details in a html table
  5. public Products = [
  6. {Name:'Hp', title: 'HP ENVY Laptop - 15t touch', price: '1099', store: 'Best Buy', model: '15-BS013DX' },
  7. { Name: 'Dell', title: 'Dell Laptop', price: '700', store: 'Amazon', model: 'I7378-3000SLV-PUS' },
  8. { Name: 'Lenovo', title: 'Lenovo Touch-Screen Laptop', price: '670', store: 'Target', model: '81A40025US' },
  9. { Name: 'Hp', title: 'HP OfficeJet Pro 6978 All-in-One Printer', price: '100', store: 'Target', model: 'T0F29A#B1H' },
  10. { Name: 'Hp', title: 'HP Laptop - 17t touch ', price: '420', store: 'Target', model: '1EZ78AV_1' },
  11. { Name: 'Dell', title: 'Dell - XPS 27" Touch-Screen All-In-One', price: '670', store: 'Target', model: 'BBY-311C3FX' },
  12. { Name: 'Dell', title: 'Dell - Inspiron 21.5" Touch-Screen All-In-One', price: '469.90', store: 'Target', model: 'I3265-A067BLK-PUS' },
  13. { Name: 'Lenovo', title: 'Lenovo - 520-24AST 23.8" Touch-Screen All-In-One', price: '679.99', store: 'Target', model: 'F0D3000EUS' },
  14. { Name: 'Dell', title: 'Dell - XPS 2-in-1 13.3" Touch-Screen Laptop', price: '1199.99', store: 'Target', model: 'XPS9365-7086SLV-PUS' }
  15. ];
  16. constructor() {
  17. }
  18. }
Bind a ProductHeader array object to a drop-down list with *ngFor
I have used *ngFor to iterate the items in an array object. For displaying a value, use {{object.key}} (this is nothing but {{ prod.name}})
  1. import { Component, } from '@angular/core';
  2. @Component({
  3. selector: 'my-product-view',
  4. styleUrls: ['styles.css'],
  5. template: `
  6. <div>
  7. Select Product :
  8. <select>
  9. <option *ngFor="let prod of ProductHeader">{{prod.name}} </option>
  10. </select>
  11. ',
  12. })
  1. export class ProductComponent {
  2. public ProductHeader = [{ name: 'Hp' }, { name: 'Dell'}, { name: 'Lenovo' }];
  3. constructor() {
  4. /// this constructor will call once the ProductComponent class object is ready.
  5. this.getProducts();
  6. }
  7. getProducts() {
  8. return this.ProductHeader;
  9. }
  10. }
Call a method on selected dropdown list item

Whenever a user changes a value in a dropdown list, the ngModelChange changes will call a method.
  1. <select [(ngModel)]="ProductHeader.name" (ngModelChange)="SearchProduct(ProductHeader.name)">
  2. <option *ngFor="let prod of ProductHeader">{{prod.name}} </option>
  3. </select>
Create a serach ProductDetails method in a Component class as below.
  1. export class ProductComponent {
  2. //Create a object for storing filter data and bind to html table.
  3. public ProductDetails: object = [];
  4. //filter product details on name and return productDetails object.
  5. SearchProduct(name: string) {
  6. let obj = this.Products.filter(m => m.Name == name);
  7. this.ProductDetails = obj;
  8. return this.ProductDetails;
  9. }

  10. }
Bind Product details object to an HTML table
Create an HTML table with <tr> and <td> to display the product title, store, price, and model.
  1. <table class="TFtable">
  2. <tr >
  3. <th>Name</th> <th>Store</th> <th>Price</th> <th>Model</th>
  4. </tr>
  5. <tr *ngFor="let Prod of ProductDetails">
  6. <td>{{Prod.title}}</td>
  7. <td>{{Prod.store}}</td>
  8. <td>{{Prod.price}}</td>
  9. <td>{{Prod.model}}</td>
  10. </tr>
  11. </table>
Here is the complete code.
  1. import { Component, } from '@angular/core';
  2. @Component({
  3. selector: 'my-product-view',
  4. styleUrls: ['styles.css'],
  5. template: `
  6. <div>
  7. Select Product :
  8. <select [(ngModel)]="ProductHeader.name" (ngModelChange)="SearchProduct(ProductHeader.name)">
  9. <option *ngFor="let prod of ProductHeader">{{prod.name}} </option>
  10. </select>
  11. <div *ngIf="ProductHeader.name">
  12. <h5>You have selected: {{ProductHeader.name}} Product </h5>
  13. </div>
  14. </div>
  15. <div>
  16. <h4>Product Details:</h4>
  17. <table class="TFtable">
  18. <tr >
  19. <th>Name</th> <th>Store</th> <th>Price</th> <th>Model</th>
  20. </tr>
  21. <tr *ngFor="let Prod of ProductDetails">
  22. <td>{{Prod.title}}</td>
  23. <td>{{Prod.store}}</td>
  24. <td>{{Prod.price}}</td>
  25. <td>{{Prod.model}}</td>
  26. </tr>
  27. </table>
  28. </div>
  29. `,
  30. })
  31. export class ProductComponent {
  32. gi
  33. //Create a object for storing filter data and bind to html table.
  34. public ProductDetails: object = [];

  35. //filter product details on name and return productDetails object.
  36. public ProductHeader = [{ name: 'Hp' }, { name: 'Dell'}, { name: 'Lenovo' }];
  37. public Products = [
  38. {Name:'Hp', title: 'HP ENVY Laptop - 15t touch', price: '1099', store: 'Best Buy', model: '15-BS013DX' },
  39. { Name: 'Dell', title: 'Dell Laptop', price: '700', store: 'Amazon', model: 'I7378-3000SLV-PUS' },
  40. { Name: 'Lenovo', title: 'Lenovo Touch-Screen Laptop', price: '670', store: 'Target', model: '81A40025US' },
  41. { Name: 'Hp', title: 'HP OfficeJet Pro 6978 All-in-One Printer', price: '100', store: 'Target', model: 'T0F29A#B1H' },
  42. { Name: 'Hp', title: 'HP Laptop - 17t touch ', price: '420', store: 'Target', model: '1EZ78AV_1' },
  43. { Name: 'Dell', title: 'Dell - XPS 27" Touch-Screen All-In-One', price: '670', store: 'Target', model: 'BBY-311C3FX' },
  44. { Name: 'Dell', title: 'Dell - Inspiron 21.5" Touch-Screen All-In-One', price: '469.90', store: 'Target', model: 'I3265-A067BLK-PUS' },
  45. { Name: 'Lenovo', title: 'Lenovo - 520-24AST 23.8" Touch-Screen All-In-One', price: '679.99', store: 'Target', model: 'F0D3000EUS' },
  46. { Name: 'Dell', title: 'Dell - XPS 2-in-1 13.3" Touch-Screen Laptop', price: '1199.99', store: 'Target', model: 'XPS9365-7086SLV-PUS' }
  47. ];
  48. constructor() {
  49. this.getProducts();
  50. }
  51. getProducts() {
  52. console.log("getProducts");
  53. return this.ProductHeader;
  54. }
  55. SearchProduct(name: string) {
  56. let obj = this.Products.filter(m => m.Name == name);
  57. this.ProductDetails = obj;
  58. return this.ProductDetails;
  59. }
  60. }
Output