简体   繁体   English

使用角度ngfor和ngif的逗号分隔列表

[英]Comma separated list using angular ngfor and ngif

I have a comma separated list that displays an array of four items. 我有一个逗号分隔列表,显示一个包含四个项目的数组。

What I'm trying to do is this: 我想要做的是这样的:

item1
item1, item2
item1, item2, item3
item1, item2, item3, item4 ...

What is happening is this: 这是怎么回事:

item1,
item1, item2,
item1, item2, item3,
item1, item2, item3, item4 ...

Here is my code: 这是我的代码:

<span *ngFor="let item of record.referrerItemList; let i=index">
    <span *ngIf="i <= 3">{{item}}</span><span class="list-format" *ngIf="i < 3">&#44;&nbsp;</span>
    <span *ngIf="(i > 3) && (i < 5)" class="hellip-format">&hellip;</span>
</span>

Here is some list results: 以下是一些列表结果:

Item1,  Item2,  Item3,  Item4 …
Item1, 
Item1,  Item2,  Item3,  Item4 …
Item1,  Item2, 

You can use *ngFor 's last exported value to avoid adding the comma to the last element: 您可以使用*ngForlast导出值来避免将逗号添加到最后一个元素:

<span *ngFor="let item of record.referrerItemList; let i=index; let isLast=last">
    <span *ngIf="i <= 3">{{item}}</span><span class="list-format" *ngIf="!isLast && i < 3">&#44;&nbsp;</span>
    <span *ngIf="(i > 3) && (i < 5)" class="hellip-format">&hellip;</span>
</span>

For more info, see https://angular.io/api/common/NgForOf . 有关详细信息,请参阅https://angular.io/api/common/NgForOf

In your example you seem to want to eliminate the final commas and have an ellipses if the length of the list exceeds four items. 在您的示例中,如果列表的长度超过四个项目,您似乎想要消除最终的逗号并使用省略号。

You can use the last exported template variable to detect if you're on the last element. 您可以使用last导出的模板变量来检测您是否在最后一个元素上。 This is the best condition to test for to prevent displaying a separator after the last element. 这是测试以阻止在最后一个元素之后显示分隔符的最佳条件。

You can use the SlicePipe to display up to four items and in the last element detect if you need ellipses because the length of the source array is greater than four. 您可以使用SlicePipe显示最多四个项目,并在最后一个元素中检测是否需要省略号,因为源数组的长度大于四。

The behavior in the example will not display ellipses if there are only four items in the list. 如果列表中只有四个项,则示例中的行为将不显示省略号。 You can change it if you want. 如果需要,您可以更改它。

<span *ngFor="let item of record.referrerItemList | slice:0:4; let $last=last">
    {{item}}
    <span *ngIf="!$last; LastElem" class="list-format">&#44;&nbsp;</span>
    <ng-template #LastElem>
        <span *ngIf="record.referrerItemList.length > 4" class="hellip-format">
            &hellip;
        </span>
    </ng-template>
</span>

just a little change should work, in place of i < 3 i!=record.referrerItemList.length-1 will work because only the last one you don't want the comma. 只需i!=record.referrerItemList.length-1一点改动就可以了,代替i < 3 i!=record.referrerItemList.length-1会起作用,因为只有最后一个你不想要逗号。

<span *ngFor="let item of record.referrerItemList; let i=index">
    <span *ngIf="i <= 3">{{item}}</span><span class="list-format" *ngIf="i!=record.referrerItemList.length-1">&#44;&nbsp;</span>
    <span *ngIf="(i > 3) && (i < 5)" class="hellip-format">&hellip;</span>
</span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM