我正在尝试使用连续循环打印逗号分隔值但获取双逗号。
更新
假设我的数组中有这样的数据:
var carModel = [model:"1",model:"2"];
var carNames = [Name:"Abc",Name:"Xyz"];
var Price = 1000;
预期输出:
1 as Car1,2 as Car2,Abc as Name1,Xyz as Name2,1000 as price
代码:
<div ng-repeat="data in carModel">
{{data.model}}
<div>as</div>
Car{{ $index + 1 }}
<span ng-if="!$last">,</span>
</div>
<div ng-repeat="data in carNames">
{{data.Name}}
<div>as</div>
Car{{ $index + 1 }}
<span ng-if="!$last">,</span>
</div>
<span ng-if="carModel.length > 0 || carNames.length > 0" > , </span>
<div>
{{ Price }}
<div>as</div> price
</div>
现在这给了我以下输出,其中包含双逗号:
1 as Car1,2 as Car2,Abc as Name1,,Xyz as Name2,1000 as price
答案 0 :(得分:4)
<div ng-repeat="data in cars">
{{data.model}}
<div>as</div>
Car{{ $index + 1 }}
<span ng-if="!$last">,</span>
</div>
$last可用于了解当前元素是否是您收藏的最后一个元素。
答案 1 :(得分:2)
其他可能的非角度解决方案:
String.join()
在javascript中汇总字符串。使用基于CSS的解决方案。例如,使用:last-of-type
pseudo selector。例如:
<div ng-repeat="data in cars" class="car">{{data.model}}
<div>as</div>
Car{{ $index + 1 }}
</div>
的CSS:
.car::after {
content: ',';
}
.car:last-of-type::after {
content: '';
}