i have observbale made from array of form controls in angular.i am using dropdowns and inputs to sum up all the currencies in relation to one currency and it works but my issue is that when i want to update the field itself, value of sum is still not changed because of http request delay. i want to set value of field when all the code is finished executing and sum has its final form.
let sum = 0;
let arrayOfControls = [];
for (let control of this.currencies.controls) {
arrayOfControls.push(control);
}
const myObservable = from(arrayOfControls);
const myObserver = {
next: (control) => {
let currency = control.get('dropdown').value;
let amount = Number(control.get('amount').value);
if (currency && amount) {
this.updateRate(currency).subscribe((serverData) => {
let rate = serverData['rates'][this.dropdown2.value];
sum += amount * rate;
// sum changes here.
});
}
},
complete: () => {
// when i try to set value of field, sum has not changed yet due to delay.
this.amount2.setValue(sum.toFixed(2));
},
};
myObservable.subscribe(myObserver);