i'm making a little page that is supposed to display browser extensions.
here is the service responsible for getting data from a json file:
constructor(private http: HttpClient) {}
getData(): Observable<Extension[]> {
return this.http.get<Extension[]>("data.json");
}
here is the parent component called main that uses the service:
extensions: Extension[] = [];
constructor(private dataService: Data) {
}
ngOnInit() {
this.dataService.getData().subscribe((result)=> {
this.extensions = result;
})
}
to test if i was getting the data, i had a method with "console.log(this.extensions)" and a button calling a method, and i got the data, so all is well here.
now i want to give this data to child component called list. so in main html i have this:
<app-list [data] = "extensions"></app-list>
and in list.ts i have this:
data = input<Extension[]>()
extList: Extension[] | undefined = [];
ngOnInit(){
this.extList = this.data();
}
when i try to console.log(this.extList) here, i am getting an empty array.
i am thinking it's because i am reading the input signal before the data is actually received but i thought ngOnInit is called after all the components are constructed and data exchanged so i don't know what to do.