Maureen Moore

Maureen Moore

  • NA
  • 206
  • 0

Table cells not rendering as desired for ngFor

Jul 11 2020 8:16 AM
I am trying to render a dynamic table with the ngFor directive. It's not rendering as desired.
This is my html:
  1. <form name="yourForm">  
  2. <table>  
  3. <tr *ngFor="let item of items">  
  4. <td>{{item[1]}} </td>  
  5. <td>{{item[0]}} </td>  
  6. <td>{{item[2]}}</td>  
  7. </tr>  
  8. </table>  
  9. </form>  
This is in my controller:
  1. export class ShoppingCartComponent implements OnInit {  
  2. items = [];  
  3.   
  4. onSubmit(quantity, product_name, product_price){  
  5.   
  6. localStorage.setItem('items', JSON.stringify(quantity, product_name, product_price));  
  7.   
  8. this.items.push( String(quantity), product_name, product_price );  
  9. }  
  10. }  
It is rendered as follows:
  1. <table>  
  2. <tr>  
  3. <td>quantity</td>  
  4. <td></td>  
  5. <td></td>  
  6. </tr>  
  7. <tr>  
  8. <td>product_name</td>  
  9. <td></td>  
  10. <td></td>  
  11. <tr>  
  12. <td>product_price</td>  
  13. <td></td>  
  14. <td></td>  
  15. </tr>  
  16. </tr>  
  17. </table>  
My desired rendering is:
  1. <table>  
  2. <tr>  
  3. <td>quantity</td>  
  4. <td>product_name</td>  
  5. <td>product_price</td>  
  6. </tr>  
  7. </table>  

Answers (6)