如何在模板中定义数组(不使用.ts) [英] how to define an array in template (not use .ts)

查看:83
本文介绍了如何在模板中定义数组(不使用.ts)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道最好的方法是从.ts定义变量.但是在这种情况下,我需要能够从给定的数字开始执行 * ngFor ,其中该数字将是具有该位置数的数组.

I know the best way is to define a variable from the .ts. But in this case I require to be able to do an *ngFor from a given number, where this number will be an array with this number of positions.

我的想法是这样的:

<ng-container * ngFor = "let item of new Array(number)>
  ...
</ng-container>

例如:number = 3

for example: number =3

<ng-container * ngFor = "let item of new Array(3)>
  repetat 3 times
</ng-container>

有可能吗?

推荐答案

这里是使用字符串的一种方法.初始字符串'x'被重复所需的次数.然后通过在每个字符上分割字符串来获得数组.

Here is one way, using a string. An initial string 'x' is repeated the desired number of times. The array is then obtained by splitting the string on each character.

<ng-container *ngFor="let item of 'x'.repeat(count).split('')">
  ...
</ng-container>

请参见此stackblitz 演示.

这篇关于如何在模板中定义数组(不使用.ts)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆