我的目标是从数组的服务数组中获取任何索引的对象数组,并通过* ngFor
循环遍历该对象数组FILE.TS
services: Array<Servizio[]> = [];
FILE.HTML这是我希望能够做到的,但我的列表中没有显示
<li *ngFor="let servizio of services[0]">
<a href="#">{{servizio.name}}</a>
</li>
答案 0 :(得分:1)
ngFor适用于数组,你试图使用数组的第一个元素作为对象,你应该这样做
<li *ngFor="let servizio of services">
<a href="#">{{servizio.name}}</a>
</li>
答案 1 :(得分:1)
我将解决方案发布到我的问题,以防它对任何人都有帮助:
<ul class="nav nav-list">
<span *ngFor="let servizio of services; let idx = index">
<span *ngIf="idx === 2">
<li *ngFor="let ser of servizio">
<a href="#">{{ser.name}}</a>
</li>
</span>
</span>
</ul>
答案 2 :(得分:0)
试试这样:
我的例子:
模板
<ul>
<li *ngFor="let servizio of services[0]?.subMenuItem">
<a href="#">{{servizio.name}}</a>
</li>
</ul>
打字稿:
services: Array<any[]> = [{
"name": "xyz",
"subMenuItem": [{
"name": "abc"
}, {
"name": "cde"
}],
"icon": "home"
}, {
"name": "pqr",
"subMenuItem": [{
"name": "abc"
}, {
"name": "cde"
}],
"icon": "home"
}];