I'm creating an app with a kind of gamefication and every task the user completes I add a string to an array achievements. I was using Observables in my Gamefication service to update the achievements array so the UI would update as well, changing the color of some "achievements badges"
The thing is.
I realized that if I simply assign an array in my component consuming my service with the reference of the array in the service like this:
gamefication.page.ts
export class GameficationPage implements OnInit {
gameficationCards: GameCard[];
constructor(private gameficationService: GameficationService) { }
ngOnInit() {
this.gameficationCards = this.gameficationService.getAvailableCards();
}
}
And the service like this: gamefication.service.ts
gameficationCards: string[] = [
'acheivement1',
'achievement2',
...
]
getAvailableCards() {
return this.gameficationCards;
}
Whenever I update the gameficationCards array in the service, my page, consuming the gamefication service , and my UI updates as well. I'd like to know why does it work and why should I use an observable for changing values if I can just simply update my array and it will reflect in the UI.
Thank you.