0
Hello!
I have to request an api, but the api values change dynamically.
was giving a read on Observables, and I can’t tell what I’m doing wrong
the function is already of the Observable type... but does not update the values...
this is my ts:
getData() {
this.walletService.getWalletFinancialInformation()
.subscribe((res) => {
this.patrimonioAtual = res['financialInformationBean'][0].patrimonioOnline.value;
this.patrimonioAtual = this.formatNumber(this.patrimonioAtual);
});
}
my service:
getWalletFinancialInformation() {
return this.httpClient.get('minhaUrlSecreta');
}
and finally my html:
<div class="money-daily">
<span class="button-default">R$ {{ patrimonioAtual }},00</span>
</div>
Do you need the value to be changed periodically in a time interval? Is the first request receiving and displaying the value normally? Which version of Angular is using?
– Leonardo Getulio
ola, I am using angular 7, the first request returns the value correctly, but when the value the api returns is changed, the variable is sent with the initial value
– Igor
Are you making new requests on the API? Because API as a rule is stateless in general, you must re-request for the client to receive the change in value.
– Leonardo Getulio
but with the observable, the exchange should be dynamic?
– Igor
No, the observable sends periodic updates of objects persisted in browser memory, date Storage, etc. For example, you define an object in a global variable, and put an observable in it, all children objects with observable will receive update from the global object, however the global object must request periodically in your API to receive a new value, when you set a value for it, it will shoot for all children who are listening to changes, in case they made a "Subscription" on top of it which is an "observable"
– Leonardo Getulio
If you only have one place in the code to receive this value of the current patrimony it is interesting to fire a periodic event, of type setTimeout(() => { this.getData(); }, 30000); for 30s.
– Leonardo Getulio
but using the timeout or interval can result in a crash of the api
– Igor