angular - Resolve id in pipe with a service -
i resolve id pipe.
i think isn't best way call service/api every time?
is better store countries before? how return 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[]): { 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 need return observable. when call subscribe() subscription returned. need return transform() hence added return
export class resolvepipe implements pipetransform { constructor(public _formdataservice: formdataservice) {} transform(value: number, args: any[]): { // added `return` , changed `subscribe` `map` return this._formdataservice.getcountry(value).map( data => { console.log(data[0].name); return data; // `{ }` explicit `return` required }); ) } } then can use pipe like
<div>{{someid | myidpipe | async}}</div> or
<some-comp [idprop]="someid | myidpipe | async></some-comp> the async pipe subscribes observable returned transform , uses resulting value binding.
Comments
Post a Comment