Problem: Trying to iterate an array of objects in angular but only appears the first one. The service works, I'm getting the full response.
The angular service where I execute a get for all the assets:
getAsset(asset_type):Observable<Asset[]>{
return this.http.get<Asset[]>(`${this.find}/${this.domain_id}/assets?asset_type=${asset_type}`, httpOptions);
}
The model of the Asset
export class Asset{
asset_name: string;
mac_address: string;
floor: number;
location: string;
temperature: number;
battery: number;
timestamp_tlm: string;
timestamp_geo: string;
}
The component.ts where i call the service and send the corresponding parameter.
ngOnInit() {
this.whereisService.getAsset(this.asset_type).subscribe(assets => {
if(Array.isArray(assets)){
this.assets = assets;
}
else if(typeof assets === 'string' ){
this.assets = [];
}
else{
this.assets = [assets];
}
});
}
}
The component.html
<div class="text-center">
<ul id="ticket" class="list-unstyled my-2">
<li class="btn w-100 bg-primary text-center text-white mx-0 my-2 display-block" *ngFor="let asset of rows_transformed let i = index">
<p class="h5"> Name: {{ asset?.asset_name }}</p>
<p class="h5"> Tipo: {{ asset?.asset_type }}</p>
<p class="h5"> Mac adress: {{ asset?.mac_address }} </p>
<p class="h5"> Piso: {{ asset?.floor }} </p>
<p class="h5"> Localização: {{ asset?.location }} </p>
<p class="h5"> Hora: {{ asset?.timestamp_tlm }} </p>
</li>
</ul>
</div>
Response JSON from API
{
"code": 200,
"data": [
{
"mac_address": "AC233F52BD17",
"floor": -3,
"asset_name": "Tripés microfone 1",
"asset_type": "Carro trasnporte",
"location": "Armazem 2",
"temperature": 22.0,
"battery": 74.0,
"timestamp_tlm": "2019-11-22 10:17:49.563121+00:00",
"timestamp_geo": "2019-11-22 10:17:49.563266+00:00"
},{...}
]
}
*ngFor="let asset of rows_transformed let i = index"