I have a C# api endpoint that returns a List<Dictionary<string, object>>
.
The problem here is this list is dynamic, so I cannot say what the elements could be so I could populate an html list (say with angular) with it, i.e.
JavaScript
x
4
1
<ul *ngFor="let data of dataList">
2
<li><span>{{data.propertyName}}<span/> -- <span>{{data.propertyValue}}<span/><li>
3
<ul>
4
Another data from the endpoint could have more data and different list properties, so instead of propertyName
and propertyValue
it could be employeeID
, employeeName
, dateOfBirth
etc.
How do I populate the list with only one list markup and api endpoint?
Advertisement
Answer
If I understand your objective correctly, then in Angular you’ll have the property:
JavaScript
1
2
1
dataList: object[]; // data model is unknown
2
In that case you can:
JavaScript
1
4
1
keys: string[];
2
3
this.keys = Object.keys(dataList[0]); // once data is retrieved you can extract its model
4
And in your HTML you can for example:
JavaScript
1
6
1
<ul *ngFor="let data of dataList">
2
<li *ngFor="let prop of keys">
3
<span>{{ prop }}: {{ data[prop] }} | <span/>
4
<li>
5
<ul>
6