首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角5 *ngIf在没有内容的情况下呈现div

角5 *ngIf在没有内容的情况下呈现div
EN

Stack Overflow用户
提问于 2018-06-25 15:25:31
回答 1查看 441关注 0票数 1

我在用

角5.2

固定

使用*ngIf isContent else noContent,只有当数据被填充时,我才会尝试呈现一个可观察的结果。逻辑并没有触及else语句。即使在没有数据的情况下,它也在呈现isContent。我已经浏览了这些类似的堆栈溢出问题,看起来我的语法是正确的。我做错了什么?

这是密码。

代码语言:javascript
复制
<ng-container *ngIf="months2025 | async as months; else nocontent">

  #### RENDERING REGARDLESS OF MONTHS2025 ####
  <div class="column">
    <h1>2025</h1> #### <-- THIS SHOWS UP ####
    <ul>
      <li *ngFor="let month of months">
        <a href={{month.url}}> {{ month.fileName }} </a>
      </li>
    </ul>
  </div>

</ng-container>

#### NOT RENDERING WHEN NOCONTENT ####
<ng-template #nocontent>
</ng-template>

这是component.ts

代码语言:javascript
复制
export class MinutesComponent {
  monthsArray2025: AngularFirestoreCollection<any>;
  months2025: Observable<any[]>;

  constructor(private afs: AngularFirestore) {
    this.monthsArray2025 = afs.collection<any>('minutes', ref => ref.where('year', '==', 2025);
    this.months2025 = this.monthsArray2025.valueChanges();
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-26 01:57:14

因此,有一个关于这个主题的Github问题。目前,我找到的最简单的解决方法是一对*ngIf容器。

代码语言:javascript
复制
<ng-container *ngIf="months2025 | async as months">
  <ng-container *ngIf="months.length > 0; else nocontent">
    <div class="column">
      <li *ngFor="let month of months">
        <a href={{month.url}}> {{ month.fileName }} </a>
      </li>
    </div>
  </ng-container>
</ng-container>

<ng-template #nocontent>
</ng-template>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51026990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档