I will resolve an ID with a pipe.
I think it isn't the best way to call the service/api every time?
Is it better to store all countries before? How could I return the country name within pipe?
service:
getCountry(id:number) {
return this._http.get(this.apiUrl + "/country/" + id).map(res => res.json());
}
pipe:
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>
First you need to return an
Observable
. When you callsubscribe()
aSubscription
will be returned. Also you need to actually return something fromtransform()
hence the addedreturn
then you can use the pipe like
or
The
async
pipe subscribes to theObservable
returned fromtransform
and uses the resulting value for the binding.