ngFor Observable数组

时间:2019-02-21 15:54:31

标签: angular ngfor

我想在Angular 7模板中使用Observable<Array<Observable<Steward>>>来展示*ngFor。您了解以下“解决方案1”有什么问题吗?

解决方案1: 我尝试了以下解决方案,但仍有效,但我遇到的问题是,这些项目以很高的频率刷新,例如,使用户无法选择输出:

<div *ngIf="term.stewards$ | async as stewards">
    <span *ngIf="stewards?.length > 0">
      <dt><b class="text-muted" placement="top"
        ngbTooltip="Persons responsible for the book. Any changes will be reviewed/approved by these persons.">STEWARDS</b></dt>
      <dd>
        <ul class="list-unstyled">
          <li *ngFor="let steward$ of stewards">
            <span *ngIf="steward$ | async as steward">
              {{steward.getName()}} ({{steward.principalId}})
            </span>
          </li>
        </ul>
      </dd>
  </span>
</div>

解决方案2:然后,我将解决方案更改为使用将Array<Observable<Steward>>转换为Array<Steward>的打字稿方法,并且使它可以正常工作而不会闪烁。

<div *ngIf="term.stewards$ | async as stewards">
    <span *ngIf="stewards?.length > 0">
      <dt><b class="text-muted" placement="top"
        ngbTooltip="Persons responsible for the book. Any changes will be reviewed/approved by these persons.">STEWARDS</b></dt>
      <dd>
        <ul class="list-unstyled">
          <li *ngFor="let steward of getObjectArray(stewards)">
            <ng-container *ngIf="steward; else loaderIcon">
                {{steward.getName()}} ({{steward.principalId}})
            </ng-container>
          </li>
        </ul>
      </dd>
  </span>
</div>

下面是打字稿功能getObjectArray(stewards)

getObjectArray(obsArray: Array<Observable<any>>): Array<any> {
  return obsArray.map(obs => {
    let object = null;
    obs.subscribe(msg => object = msg);
    return object;
  });
}

0 个答案:

没有答案