首页 > 解决方案 > 有没有办法延迟 ngx-bootstrap 手风琴的打开?

问题描述

<accordion [closeOthers]=true>
  <accordion-group *ngFor="let activity of activities" [heading]="activity.Name" (click)="openPanel(activity)" (isOpenChange)="openStatusChange($event)">
    <ul *ngFor="let chemical of chemicals">
      <li>{{chemical.BrandName}}</li>
    </ul>
    <div *ngIf="!chemicals?.length > 0">No chemicals associated with this activity type.</div>
  </accordion-group>
</accordion>

单击手风琴标题时,它会打开并运行并触发“openPanel()”,它是一个 http.get,然后填充面板。如果数组返回空,*ngIf 将显示“没有关联的东西”消息。

问题是在手风琴打开和阵列被填充之间有一个非常小的延迟,因此当手风琴打开时,化学品阵列总是空的。这使得“没有关联的东西”消息出现大约半秒钟,然后填充列表。所以我想知道是否有办法延迟打开直到阵列被填充,或者欢迎提出建议。

标签: ngx-bootstrap

解决方案


您可以为 ngIf 使用布尔标志或函数。当 http 承诺返回成功时设置标志,这样 ngIf 仅在 http 调用承诺解决后触发。

就像是:

http.get('url').subscribe(response => {
  show = true;
});

推荐阅读