[英]Preserve trailing whitespace in html template of angular component?
I want to display a comma separated list of names like test1, test2, test3
.我想显示一个逗号分隔的名称列表,例如
test1, test2, test3
。 I am using something like below:我正在使用类似下面的东西:
<div style="display: inline-block;" *ngFor="let object of objectList; let i = index;">
{{object.name}}{{((i+1) === objectList.length) ? '' : ', '}}
</div>
All the trailing whitespaces are removed and result is test1,test2,test3
.所有尾随空格都被删除,结果为
test1,test2,test3
。 I was using  
but I read at some places that it was not recommended to use.我正在使用
 
,但我在一些不建议使用的地方阅读。 How should I preserve these whitespaces?我应该如何保留这些空格?
edit: code didn't have that style tag earlier (which is in a separate.css file in the actual code but placing it inline for the sake of the question)编辑:代码之前没有那个样式标签(它在实际代码中的单独的.css 文件中,但为了问题而将其内联)
I'd suggest two changes.我建议进行两个更改。
<div>
tag.<div>
标记中。 So it'd look like following<div>test1, </div>
<div>test2, </div>
<div>test3, </div>
<div>test4, </div>
<div>test5</div>
Whereas you might need to enclose all the elements in a single <div>
container like而您可能需要将所有元素包含在单个
<div>
容器中,例如
<div>test1, test2, test3, test4, test5</div>
For that you could iterate the array in a <ng-container>
tag.为此,您可以在
<ng-container>
标记中迭代数组。 It'll be commented out in the final DOM and would render additional elements.它将在最终的 DOM 中被注释掉并渲染其他元素。
last
local variable of the *ngFor
directive instead of checking using the index and the array's length.*ngFor
指令的last
局部变量,而不是使用索引和数组长度进行检查。 Try the following尝试以下
<div>
<ng-container *ngFor="let object of objectList; let last = last;">
{{ object.name }}{{ last ? '' : ', ' }}
</ng-container>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.