1
パイプでIDを解決します。サービスでパイプ内のIDを解決する
私はサービス/ apiを毎回呼び出す最良の方法ではないと思いますか?
以前にすべての国を保存する方が良いですか?パイプ内で国名を返すことはできますか?
サービス:
getCountry(id:number) {
return this._http.get(this.apiUrl + "/country/" + id).map(res => res.json());
}
パイプ:
export class ResolvePipe implements PipeTransform {
constructor(public _formDataService: FormDataService) {}
transform(value: number, args: any[]): any {
this._formDataService.getCountry(value).subscribe(
data => console.log(data[0].name)
)
}
}
EDIT:
<tr (click)="showProfile(customer)" *ngFor="let customer of (customers | search:term)">
<td>...</td><td>{{customer.country_id | resolve | async}}</td>
</tr>
ありがとうございます、作品は非常に遅いです。パフォーマンスを向上させるために私は何ができますか? – robert
ここから遅いと言うのは難しいです。サーバーからの応答には長い時間がかかりますか?パイプが頻繁に実行されるため、アプリケーションが遅くなります。私はここからはわかりません。 'FormDataService'の中では、http://stackoverflow.com/questions/36271899/what-is-the-correct-way-to-share-the-result-of-an-angular-2-で議論されているように、キャッシュを維持するかもしれません。 http-network-call-in –
はい、1000回くらいです。キャッシュがこの問題を解決するはずです。 – robert