我在ngOnInit上调用了两次可观察到的异步管道,第二次在其他订阅内部调用了异步管道。因此,问题在于异步管道可观察的调用仅在第二次之后才被第一次调用。
通过异步管道可观察到
HTML代码:
<ul class="dropdown-menu">
<li *ngFor="let item of accountTypes | async">
<a href="javascript:void(0)">{{item.Code}}</a>
</li>
</ul>
角度代码:
accountTypes: Observable<Array<AgencyInfo>>;
ngOnInit() {
// first call -- working fine
this.accountTypes = this.accountsService.getAgency(-1);
}
// call method on button click
getGBACSalesPersonCode() {
this.authenticationService.getGBACSalesPersonCodes().subscribe((res:
any[]) => {
// second call -- not calling api(error)
this.accountTypes = this.accountsService.getAgency(5);
});
}
第二个调用应调用API。