[英]How to show in template property from array of objects
我只是嘗試在模板中顯示屬性的值。 但目前什么都沒有顯示。
所以這是組件:
export class ServerStatusComponent implements OnInit {
snovieCollection: SnovietatusDto = {};
constructor(private snovierStatus: snovieStatusService) {}
ngOnInit(): void {
this.sensorStatus
.getSensorStatuses()
.pipe(
map((data) => {
console.log(data.cameraSensors);
})
)
.subscribe((status) => {
});
}
}
這是模板:
<p>Camera sensoren</p>
<tr *ngFor="let camera of snovieStatusCollection.key|keyvalue">
test
<h3> {{camera | json}}</h3>
</tr>
所以我只想在模板中顯示鍵的值。 console.log 返回:
0: {key: "T", latestTimestamp: "2021-03-12T10:09:00Z"}
所以我沒有得到任何錯誤。 但也沒有顯示任何內容。
兩件事情:
map
返回任何東西。 所以undefined
將被發送到訂閱。 使用tap
來代替副作用。this.sensorStatusCollection
。export class ServerStatusComponent implements OnInit {
sensorStatusCollection: SensorStatusDto = {};
constructor(private sensorStatus: SensorStatusService) {}
ngOnInit(): void {
this.sensorStatus
.getSensorStatuses()
.pipe(
tap((data) => { // <-- `tap` here
console.log(data.cameraSensors);
})
)
.subscribe((status) => {
this.sensorStatusCollection = status; // <-- assign here
});
}
}
this.sensorStatusCollection
類型的SensorStatusDto
export class ServerStatusComponent implements OnInit {
sensorStatusCollection: SensorStatusDto[] = [];
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.