I have a service in an Angular 2 app, events.service.ts:
const EVENTS = {
1512205360: {
event: 'foo'
},
1511208360: {
event: 'bar'
}
}
@Injectable()
export class EventsService {
getEvents() {
return EVENTS;
}
deleteEvent(timestamp) {
delete EVENTS[timestamp];
}
}
I have a home component which calls this service's getEvents method in its ngOnInit hook:
getEvents(): void {
this.events = this.eventsService.getEvents();
this.eventKeys = Object.keys(this.events);
}
ngOnInit(): void {
this.getEvents();
}
The home component's template simply loops over the events and prints them out, with a delete icon next to each one:
<event *ngFor="let eventKey of eventKeys" [timestamp]="eventKey" [title]="events[eventKey].event"></event>
This is the event component:
@Component({
selector: 'event',
templateUrl: './event.component.html',
providers: [EventsService]
})
export class EventComponent {
@Input() timestamp: string;
@Input() title: string;
constructor(private eventsService: EventsService) { }
deleteEvent(timestamp) {
this.eventsService.deleteEvent(timestamp);
}
}
Clicking the delete icon calls the deleteEvent method in the events service. My problem is that when I click the delete icon, although I can see that the event has been deleted (by console.logging the EVENTS object), the home component isn't updated and produces this error:
Cannot read property 'event' of undefined