首页 文章

* ngFor:循环的最后一个索引为null [重复]

提问于
浏览
0

这个问题在这里已有答案:

这是我的代码以最小的形式:

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

注释是一个数组,在 *ngFor 之外有5个对象 . 它在循环内迭代6次,最后一个索引变为null .

产量

enter image description here

Edit
安慰

enter image description here

有谁知道发生了什么?

2 回答

  • 0

    尝试在调试输出后添加json管道以查看数组的实际内容 .

    {{selectedPost.comments | json}}
    
  • 0

    我知道为什么会发生这样的事情 . 我把 *ngIf="!commentsLoading" 这样一级移动了,问题解决了 .

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

    我想当我在一个元素中一起使用 *ngFor*ngIf 以解决此问题时会导致问题:https://github.com/angular/angular/issues/7315

相关问题