Is there any way to display section list in Angular using *ngFor? All examples I found are using separate loops for separate sections. Thank you!
JavaScript
x
19
19
1
const DATA = [
2
{
3
title: "Main dishes",
4
data: [{name: "Pizza", type: "1"}, {name: "Pizza", type: "5"}]
5
},
6
{
7
title: "Sides",
8
data: [{name: "Pizza", type: "2"}]
9
},
10
{
11
title: "Drinks",
12
data: [{name: "Pizza", type: "3"}]
13
},
14
{
15
title: "Desserts",
16
data: [{name: "Pizza", type: "4"}]
17
}
18
];
19
Advertisement
Answer
You may use the following code on the template:
JavaScript
1
9
1
<div *ngFor="let item of DATA">
2
<h3>{{item.title}}</h3>
3
<section *ngFor="let inner of item.data" style="background-color: #f558e0;
4
width: 150px; margin-bottom: 1%;">
5
<div>{{inner.name}}</div>
6
<div>{{inner.type}}</div>
7
</section>
8
</div>
9
Tweak the styles accordingly.