【问题标题】:Remove divider after last item while using ngFor使用 ngFor 时删除最后一项后的分隔线
【发布时间】:2021-03-08 18:16:48
【问题描述】:

我正在使用此代码显示多个位置:

<div class="location" *ngFor="let item of location_array">
   {{item}} <hr>
</div>

这会导致所有位置,由水平线分隔。如何编辑它以获得完全相同的结果,但没有最后一个水平规则?

【问题讨论】:

    标签: angular ionic-framework


    【解决方案1】:

    在您的&lt;hr&gt; 上使用*ngFor 提供的last*ngIf

    <div class="location" *ngFor="let item of location_array; let lastItem = last;">
       {{item}} <hr *ngIf="!lastItem">
    </div>
    

    这是StackBlitz Demo

    【讨论】:

    • 我认为它没有按照应有的方式工作。我看到
      获取所有项目。
    • 我期待一种更复杂的方法来做到这一点,但堆栈溢出永远不会让人失望。
    【解决方案2】:

    在当前的 Angular 版本 (11) 中,语法更改为:

    <div *ngFor="let item of location_array; last as isLast">
        {{ item }} <hr *ngIf="!isLast">
    </div>
    

    https://angular.io/api/common/NgForOf#local-variables

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-10
      • 2018-02-23
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多