I just try to show the value of a property in the template. But at the moment nothing is shown.
So this is the component:
JavaScript
x
19
19
1
export class ServerStatusComponent implements OnInit {
2
snovieCollection: SnovietatusDto = {};
3
4
constructor(private snovierStatus: snovieStatusService) {}
5
6
ngOnInit(): void {
7
this.sensorStatus
8
.getSensorStatuses()
9
.pipe(
10
map((data) => {
11
console.log(data.cameraSensors);
12
})
13
)
14
.subscribe((status) => {
15
});
16
}
17
}
18
19
And this is the template:
JavaScript
1
8
1
<p>Camera sensoren</p>
2
<tr *ngFor="let camera of snovieStatusCollection.key|keyvalue">
3
4
test
5
<h3> {{camera | json}}</h3>
6
7
</tr>
8
So I just want to show in the template the value of key. And the console.log returns this:
JavaScript
1
3
1
0: {key: "T", latestTimestamp: "2021-03-12T10:09:00Z"}
2
3
So I don’t get any errors. But also nothing is shown.
Advertisement
Answer
Two things:
- You aren’t returning anything from the
map
. Soundefined
would be emitted to the subscription. Usetap
for side-effects instead. - You aren’t assigning the response to
this.sensorStatusCollection
in the subscription.
JavaScript
1
19
19
1
export class ServerStatusComponent implements OnInit {
2
sensorStatusCollection: SensorStatusDto = {};
3
4
constructor(private sensorStatus: SensorStatusService) {}
5
6
ngOnInit(): void {
7
this.sensorStatus
8
.getSensorStatuses()
9
.pipe(
10
tap((data) => { // <-- `tap` here
11
console.log(data.cameraSensors);
12
})
13
)
14
.subscribe((status) => {
15
this.sensorStatusCollection = status; // <-- assign here
16
});
17
}
18
}
19
Update: Type
- As pointed out by @TotallyNewb in the comments, the type of
this.sensorStatusCollection
needs to be an array of typeSensorStatusDto
JavaScript
1
6
1
export class ServerStatusComponent implements OnInit {
2
sensorStatusCollection: SensorStatusDto[] = [];
3
4
5
}
6