Having trouble updating an Angular2 component's variable that is tied to a variable in another service? You're not alone. I've been struggling to get it to update without constantly resetting the variable.
In my component, I have the following code inside ngOnInit:
ngOnInit(){
this.rooms = this.globals.filteredRooms;
}
Meanwhile, in my @injectable called AppGlobals, I find myself frequently writing code like this:
applyFilters() {
this.filteredRooms = this.rooms.filter(r => this.campFilters.includes(r.camp));
}
The issue arises when I want the rooms variable in my component to automatically change whenever the filteredRooms variable in appGlobals changes. Instead of this automatic update, I keep having to manually set this.rooms = this.globals.filteredRooms over and over again.
I've attempted various solutions such as trying (and failing) to create an Observable and subscribe to it, importing my component into appGlobals to trigger updates (resulting in mysterious errors), and exploring the idea of using byRef specification to establish a reference rather than making copies.
If changing to a reference is indeed necessary, how can that be achieved? How do I go about creating an Observable and subscribing to it if that's the solution? Or perhaps there's a different approach altogether that I'm missing?