[英]Append element in DOM from array
I have: 我有:
<div *ngFor="let item of items">
<h2>Hello {{item.name}}</h2>
</div>
My items: 我的物品:
items = [
{
name: 'David',
star: 5
},
{
name: 'George',
star: 2
},
{
name: 'Michael',
star: 0
},
{
name: 'Tim',
star: 1
},
]
They render: 他们呈现:
Hello Tim
etc. 等等
How to add: 如何添加:
<i class="fa fa-star"></i>
above h2 when in array: 高于h2时(在数组中):
star: 1
etc? 等等? Something like this:
像这样:
if (this.items.star == 2) {
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
}
(It's just an example "if statement" to understand what's going on) (这只是一个“ if语句”示例,用于了解发生了什么情况)
My PLUNKER: https://plnkr.co/edit/lfZT6FwenhYkQf1MUx9E?p=preview 我的朋克: https ://plnkr.co/edit/lfZT6FwenhYkQf1MUx9E ? p = preview
You can use the *ngIf
template decorator this way: 您可以通过以下方式使用
*ngIf
模板装饰器:
<div *ngFor="let item of items">
<i class="fa fa-star" *ngIf="item.star === 1></i>
<h2>Hello {{item.name}}</h2>
</div>
See more examples and read more about it here 在此处查看更多示例并了解更多信息
*ngIf: Conditionally includes a template based on the value of an expression.
* ngIf:根据表达式的值有条件地包括模板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.