-
Notifications
You must be signed in to change notification settings - Fork 26.5k
Closed
Labels
area: commonIssues related to APIs in the @angular/common packageIssues related to APIs in the @angular/common packagefeatureIssue that requests a new featureIssue that requests a new featurefeature: insufficient votesLabel to add when the not a sufficient number of votes or comments from unique authorsLabel to add when the not a sufficient number of votes or comments from unique authorsfeature: votes requiredFeature request which is currently still in the voting phaseFeature request which is currently still in the voting phase
Milestone
Description
🚀 feature request
Relevant Package
This feature request is for @angular/common regarding *ngFor.
Description
*ngFor should make it easy to output a list with separator characters in cases <li> or table rows are not desired. At the moment, you need code like the following simplified example:
<span *ngFor="let fruit of fruits; first as isFirst"><ng-container *ngIf="!isFirst">,
</ng-container>{{fruit}}</span>
- Note 1: There must be no whitespace before the
ng-container
hostingngIf
and before the closingspan
. Otherwise you will end up with a white space before the comma in the final output. - Note 2: In this simplified example, you end up with the separator character inside of the hosting element. Therefore, it is likely that the
*ngFor
directive needs to be pushed up to an additionalng-container
-element in real world use cases.
Describe the solution you'd like
*ngFor should support a separator
attribute, which contains a string to be added between pairs of entries:
<span *ngFor="let fruit of fruits; separator=', ' ">{{fruit}}</span>
There will be no comma, if the list is empty or has one entry only. But if there are more entries, a comma will be added between them.
Further use cases
The following two use cases show that the proposed feature improves readability significantly:
Use case: Tooltips on items for accessibility reasons
Current:
<ng-container *ngFor="let fruit of fruits; first as isFirst"><ng-container *ngIf="!isFirst">, </ng-container>
<span [title]="fruit.name">{{fruit.abbriviation}}</span></ng-container>
Proposed:
<span *ngFor="let fruit of fruits; separator=', ' "
[title]="fruit.name">{{fruit.abbriviation}}</span>
Result:
<span title="Apple">AP</span>,
<span title="Orange">OR</span>
Use case: Items as links
Current:
<ng-container *ngFor="let fruit of fruits; first as isFirst"><ng-container *ngIf="!isFirst">, </ng-container>
<a href="/details/{{fruit.id}}">{{fruit.name}}</a></ng-container>
Proposed:
<a *ngFor="let fruit of fruits; separator=', ' "
href="/details/{{fruit.id}}">{{fruit.name}}</a>
Result:
<a href="/details/47">Apple</a>,
<a href="/details/42">Orange</a>
Describe alternatives you've considered
The alternative is the current approach of using a nested ng-container with a *ngIf-directive.
Possible extensions of the feature request
- It might be a good idea to add a lastSeparator-attribute as well, in order to allow for a nice conclusion of the list with "and" or "or".
<span *ngFor="let fruit of fruits; separator=', ' lastSeparator=' and ' ">{{fruit}}</span>
- It might be useful to allow a template references as alternative to a string as separator, similar to the way *ngIf supports "else".
DominoPivot
Metadata
Metadata
Assignees
Labels
area: commonIssues related to APIs in the @angular/common packageIssues related to APIs in the @angular/common packagefeatureIssue that requests a new featureIssue that requests a new featurefeature: insufficient votesLabel to add when the not a sufficient number of votes or comments from unique authorsLabel to add when the not a sufficient number of votes or comments from unique authorsfeature: votes requiredFeature request which is currently still in the voting phaseFeature request which is currently still in the voting phase