I have an angular component that fetches data from the server, then transforms it before assigning it to a variable:
class Component {
data = [];
ngOnInit() {
this.store.select(data).pipe(
map(data => { this.data = this.transformData(data); })
).subscribe();
}
transformData(data) {
// data is an array of data
return data.map(item => item);
}
}
The above is a simplified version of what I'm doing, the transform method does more but it's irrelevant.
then I try to do a for loop using the data but it wont show me anything, and there are no errors being thrown either:
<div *ngFor="let item of data|async">
{{ item.prop1 }} {{ item.prop2 }}
</div>
I'm not sure why this is happening - I'm used to react where I pass in props or update the state and the component re-renders, what's the workflow in angular?
I tried this solution but it still wont show me any data:
class Component {
data = [];
ngOnInit() {
this.store.select(data).pipe(
map(data => { this.data = this.transformData(data); })
).subscribe();
}
transformData(data) {
// data is an array of data
// notice 'of()'
return of(data.map(item => item));
}
}
To clarify, I do see the value of this.data change when I console.log, but the html is not rendering the data
store?. Also, Why isn't yourComponentimplementingOnInit. The OnInit method will not be called otherwise. Use a constructor(){} to call your function if you do not intend to use ngOnInit's lifecycle event.OnInit, Angular will call the method if it exists.Angular instead inspects directive and component classes and calls the hook methods if they are defined. Angular finds and calls methods like ngOnInit(), with or without the interfaces.Here is the official docs: angular.io/guide/…