I have a parent component which is loading data from a database. This data comes from my service which returns a promise.
Component:
ngOnInit() {
this._massEmpService.fetchFilterFields()
.then(results => {
this.fields = {
areas: results['areas']['options'],
silos: results['silos']['options'],
departments: results['departments']['options'],
locations: results['locations']['options'],
segments: results['segments']['options'],
roles: results['roles']['options']
};
});
}
Service:
fetchFilterFields() {
return new Promise((resolve, reject) => {
this._http.get(this.baseUrl + '/fetchImportFields', { "headers": this.headers })
.map((result: Response) => result.json())
.subscribe((results) => resolve(results.data));
});
};
I am trying to pass my this.fields
data to a child component but when I try and use the data in the child component, it doesn't appear to have access to it.
Child Component:
@Input() fields: any;
ngAfterViewInit() {
console.log(this.fields); // No data
}
Parent HTML:
<app-import [fields]="fields"></app-import>
I believe this is an async issue but I thought using the afterViewInit
may have solved it. Any thoughts?