0

This is my code in a minimal form:

<div>
    {{ 'This is all comments Array =>'}}<br> 
    {{selectedPost.comments}}
    <div *ngIf="!commentsLoading" *ngFor="#comment of selectedPost.comments">
            {{ 'This is comment Object => ' + comment }}
    </div>
</div>

The comments is an array which has 5 objects outside the *ngFor. It iterates 6 times inside the loop and the last index becomes null.

Output

enter image description here

Edit
Console

enter image description here

Does anyone know what's happening?

Siamak Motlagh
  • 5,028
  • 7
  • 41
  • 65

2 Answers2

0

Try adding a json pipe after your debug output to see the actual content of the array.

{{selectedPost.comments | json}}
Alexander Ciesielski
  • 10,506
  • 5
  • 45
  • 66
0

I've found the problem but I don't know why such thing is happening. I moved the *ngIf="!commentsLoading" one level up like this and the problem solved.

<div *ngIf="!commentsLoading">
  {{ 'This is all comments Array =>'}}<br> 
  {{selectedPost.comments}}
  <div *ngFor="#comment of selectedPost.comments">
        {{ 'This is comment Object => ' + comment }}
  </div>
</div>

I guess it causes problem when I use *ngFor and *ngIf together in one element in order to this issue: https://github.com/angular/angular/issues/7315

Siamak Motlagh
  • 5,028
  • 7
  • 41
  • 65