I'm loading in data from another file(component) and when I console it from within a function so after an event it works:
updateSub(data) {
console.log('attempting to print the subsid array' +
this.engagementService.orgSubSidNames)
}
When I try to call out outside of this and outside of scope but within my ngOnit it consoles empty:
console.log('attempting to print the subsid array initially' +
this.engagementService.orgSubSidNames)
All of the code is included within my ngOnit. I have also included engagementService in my constructor, thats how i am accessing it:
export class EngagementFilterComponent implements OnInit {
constructor(private builder: FormBuilder, public engagementService:
EngagementService) { }
ngOnInit(): void {
console.log('attempting to print the subsid array initially' +
this.engagementService.orgSubSidNames)
}
}
In Engagement service I make the http call like so:
organizationSubsidiaries(sfid: string) {
const url = `${this.baseURL}/${ENDPOINTS.organizations}/${this.organization.id}/${ENDPOINTS.subsidiaries}`;
return this.http.get(url).map(res => res.json());
}
Then when the browser is loaded it is called like such and i create the array I want from the response:
this.organizationSubsidiaries(id)
.subscribe(data => {
let subsidiaryNames = []
data.forEach(sub => {
console.log(sub.SubsidiaryName)
subsidiaryNames.push(sub.SubsidiaryName)
})
subsidiaryNames = subsidiaryNames.filter(this.onlyUnique);
this.orgSubSidNames = subsidiaryNames;
console.log('data from subsidiaries' + JSON.stringify(this.orgSubSidNames))
});
So an example of another call using observables (what I might be missing) is here:
allEngagementAreas(): Observable<IBaseEngagement[]> {
const url = `${this.baseURL}/${ENDPOINTS.engagementAreas}`;
return this.http.get(url).map(res => res.json() as IBaseEngagement[]);
}
The array I have I want to display in the UI.
What can I do?