I know that problem that view trying to get data before it filled up. But don't know how to fix this. At the first time subscribe return nothing to the view, at the second - data from first request etc. I used this construction in the project many times, and this is the first one with such interesting result. IN the code below used POST methods.
Here is my code:
private findOtherAddresses(person: any, address: Address) {
this.scoredAddresses = [];
this.addressParam.person = person;
this.addressParam.address = address;
this.Service.findOtherPersonAddresses(person).subscribe( //look for other person addresses
adr => {
this.similarAddresses = adr;
})
this.Service.getScores(this.addressParam).subscribe(res => { //get score for other addresses
this.adressScore = res;
this.similarAddressPersonId = person;
this.addressForReplaceId = address.id;
})
for (let address of this.similarAddresses) { //restrict addresses and scores
for (let score of this.adressScore) {
let scoredAddress = new ScoredAddress;
if (address.id == score.id) {
scoredAddress.scoredAddress = address;
scoredAddress.addressScore = score.result;
this.scoredAddresses.push(scoredAddress);
}
}
}
}
View:
<div *ngIf="(scoredAddresses != null)">
<div>
<label *ngIf="(address.person == similarAddressPersonId) && (address.id == addressForReplaceId)">Other addresses: </label>
<p-dataTable *ngIf="(address.person == similarAddressPersonId) && (address.id == addressForReplaceId)" [value]="scoredAddresses">
<p-column >
<template let-item="rowData" pTemplate="body">
{{item.addressScore}}
</template>
</p-column>
<p-column>
<template let-item="rowData" pTemplate="body">
{{item.scoredAddress}}
</template>
</p-column>
</p-dataTable>
</div>
</div>
Please, help me to solve this self-created issue(((. I'll also appriciate for advice how to avoid this in the future.