具有逗号分隔值的 NgFor

NgFor with Comma separated Values

我有一个用户输入了这种格式的字符串

string = "1,5,12,66"

我只想对值 1 5 12 66 执行 ngFor,我的代码是

<div *ngFor="let number of arrString" ng-if="number!=','">
    <a class="item item-icon-right font-12">
       <b>{{number}}</b>
    </a>
</div>

我的代码显示的问题

1

5

1 (should show 12)

2

6

6 (should show 66)

如有任何帮助,我们将不胜感激

您可以使用 string.Split 方法创建一个字符串数组,并循环遍历它们

 Mystring = '1,5,12,66';
 arrString = this.Mystring.split(',');

STACKBLITZ DEMO

试试这个代码。

stringData = "1,5,12,66"
arrString = this.stringData.split(',');

<div *ngFor="let number of arrString">
    <a class="item item-icon-right font-12">
       <b>{{number}}</b>
    </a>
</div>