[英]Dynamically create array of HTML elements with typescript in Angular 2/4
I want to dynamically add divs using a loop with typescript in angular. 我想使用带有typescript的循环动态地添加div。 I want to reproduce the following pseudocode in my .ts component file:
我想在我的.ts组件文件中重现以下伪代码:
i: number = 4;
arrayofHTMLelements: html elements = [];
for i in range (1, i):
create div at index i
I'm assuming I could then slide the array of HTML elements into the .html component file using: 我假设我可以使用以下方法将HTML元素数组滑动到.html组件文件中:
<li *ngFor="let arrayofHTMLelement of arrayofHTMLelements; let i = index">{{i + 1}}: {{arrayofHTMLelements}}</li>
You should iterate over your objects and create the inner html in the template like this: 你应该迭代你的对象并在模板中创建内部html,如下所示:
const heroes = [
{ name: 'Spiderman' },
{ name: 'Superman' },
{ name: 'Superwoman!' }
]
template: `
<ul>
<li *ngFor="let hero of heroes">
<div>{{hero.name}}</div>
</li>
</ul>
`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.