I am trying to create the following component architecture. A parent component with a large template that is a placeholder for many children components. The parent component is responsable for getting data from the server. The parent module also has a service to serve data to children components. Actual data subscription is taking a place in the child component. Here is some relevant code: Generic child component:
import { Component, OnDestroy, OnInit, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
import { DataSvcService } from '../../dashboard/data-svc.service';
@Component({
selector: 'app-generalstatistics',
templateUrl: './generalstatistics.component.html',
styleUrls: ['./generalstatistics.component.css']
})
export class GeneralstatisticsComponent implements OnInit, OnDestroy, AfterViewInit {
@ViewChild('generalstatistics', {static: false}) span: ElementRef;
subscription;
generalStatistics = new Array({});
constructor(private dataSvc: DataSvcService) {}
ngOnInit() {
this.subscription = this.dataSvc.generalstatistics.subscribe(data => {
this.generalStatistics = data;
});
}
ngAfterViewInit() {
console.log(this.span);
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
Template: generalstatistics.component.html
<span #generalstatistics></span>
Parent component (implementation template):
<div class="card-body pb-0">
<app-generalstatistics></app-generalstatistics>
</div>
Parent component service: '../../dashboard/data-svc.service'
import { BehaviorSubject, Subject, Observable } from 'rxjs';
import { Injectable } from '@angular/core';
@Injectable()
export class DataSvcService {
constructor() { }
public generalstatistics = new BehaviorSubject<Array<object>>([{}]);
public get generalStatistics(): Observable<Array<object>> {
return this.generalstatistics.asObservable();
}
}
Parent component data request:
this.http.get<any>(`${environment.apiUrl}/api/
home/`)
.subscribe((data: any) => {
this.dataSvcService.generalstatistics.next(data);
});
My idea is that I want to have generalstatistics as a generic component. Actual data binding should take place in the parent component. I am trying adding something like this in the implementation:
<app-generalstatistics **#someid**></app-generalstatistics>
So I could identify it in the generalstatistics component code and assign the data to the span element. But when I check for this.span.nativeElement I dont see anything coming from the actual implementation.
My question is how do I bind data to a child component in my solution? Shuold I change the whole architecture?
Thanks