I'm trying to populate a calendar based on the response I receive from a restful service (currently returning a list of dates).
I'm calling the restful service method from the ngInit() method of the component.
ngOnInit() {
this.refreshCalendar();
for (var calendarEntryShortvar of this.calendarEntriesShort) {
console.log("cal entry short from ngInit: " + calendarEntryShortvar.start);
}
}
The above loop to print the contents fails due to an undefined length.
Here is the code for this.refreshCalendar();
refreshCalendar(){
this.calendarService.retrieveAllCalendarEntriesShort('test')
.subscribe (
response => {
console.log("printing response")
console.log(response)
this.calendarEntriesShort = response;
for (var calendarEntryShortvar of this.calendarEntriesShort) {
console.log("cal entry short: " + calendarEntryShortvar.start);
this.calendarEntry = new CalendarEntry(calendarEntryShortvar.start, calendarEntryShortvar.start,
'title event 1', this.colors.redx, this.actions);
console.log("pushing calendar entry")
this.events.push(this.calendarEntry);
}
}
)
}
The above code successfully prints the contents of the array, meaning it's executing after it's received the response from service.
Here's the code for of the service call:
retrieveAllCalendarEntriesShort(username) {
console.log("retrieveAllCalendarEntries");
return this.http.get<CalendarEntryShort[]>(`${CALENDER_API_URL}/users/${username}/calendarentries`);
}
Needless to say, the calendar's HTML is getting rendered before the calendar date array is populated. Here's the HTML component:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"/>
<angular-calendar-year-view [events]="events" [viewDate]="viewDate" [customTemplate]="Customtemplate" ></angular-calendar-year-view>
<ng-template #Customtemplate>
My custom templatex
</ng-template>
<angular-calendar-year-view
[themecolor]="themecolor"
[events]="events"
[viewDate]="viewDate"
[nothingToshowText]="nothingToshowText"
(eventClicked)="eventClicked($event)"
(actionClicked)="actionClicked($event)" >
</angular-calendar-year-view>
Somehow I need to get Angular to pause until the response is received. Is this just a question of making it a synchronous call? I'm not sure why this isn't working.