After receiving a JSON response from a server via HTTP GET, the data structure looks like this:
{
searchType: "search_1",
overview: [
"Bed",
"BedSheets",
"BedLinen",
..
]
}
The contents of the overview
array vary depending on the keyword.
To keep track of searched keywords and their responses, I have an Array named Output
, structured like this:
[
{kw: keyword1, resp: {JSON response as mentioned above}},
{kw: keyword2, resp: {another JSON response with different elements in the `overview` array}}
]
In my component.html
file, I aim to display the entries within the overview
array as Buttons.
<ul>
<li *ngFor="let eachOutput of Output">
<div *ngFor="let each of eachOutput.resp.overview">
<button *ngIf="eachOutput.resp.overview.length >=1">{{each}}</button>
</div> <!-- I want to show only the first element here -->
<!--- Then I want to display additional elements with an option like 'More'
next to the first button -->
</li>
</ul>
At present, there are keywords where the array contains numerous elements. I would prefer to display just overview[0]
as a button, along with text such as More Options
which, when clicked, reveals the other elements of overview
.
How should I correctly utilize ngFor
and ngIf
in this scenario? I've been advised against using the hidden
CSS attribute in Angular 2.