1

I am trying to convert my AngularJS application to Angular2 application.I have a nested loop in my template. But it is not working when I use Angular2 syntax.

this is my code -

<table>
 <tr *ngFor = "let lbusQuestionAnswer of ibusQuestion.ilstQuestionAnswer">
  <table> 
   <tr *ngFor = "let lbusComment of lbusQuestionAnswer.ilstComments">
    <td>
     <div>{{lbusComment.icdoComment.idoComment.commentHtml}}</div>
    </td>
   </tr>
  </table>
 </tr>
</table>

JSON enter image description here And I get below error in console log.

EXCEPTION: TypeError: Cannot read property 'ilstComments' of undefined

I know, there are a lot of similar questions like this on StackOverflow. But nothing seems to be working.

Thanks Fahad Mullaji

Fahad Mullaji
  • 1,332
  • 5
  • 15
  • 30

2 Answers2

0
<div *ngIf="ibusQuestion && lbusQuestionAnswer" >
  <table>
    ...
  </table>
</div>

Valida si los elementos de la lista existen antes de mostrarlos en la vista

0

I found solution to this here - https://stackoverflow.com/a/34833436

When I added <div *ngIf ="ibusQuestion"> then it worked fine!!

I hope this answer helps and explains the real reason behind it

Community
  • 1
  • 1
Fahad Mullaji
  • 1,332
  • 5
  • 15
  • 30