订阅第一次返回undefined

时间:2017-12-21 08:50:12

标签: angular typescript undefined subscribe

我知道在填充之前查看尝试获取数据的问题。但不知道如何解决这个问题。订阅时第一次订阅返回视图,第二次 - 第一次请求的数据等。我在项目中多次使用这种结构,这是第一次有这样有趣的结果。在下面的代码中使用了POST方法。

这是我的代码:

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);
               }
           }
       }
   }

查看:

    <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>

请帮助我解决这个自我创造的问题((。我还会建议如何在将来避免这种情况。

0 个答案:

没有答案