3

I'm trying to create Pipe Which should send a request to the server, wait for an answer, and return the result

import {Pipe, PipeTransform,  Inject} from "@angular/core";
import {translationService} from "./translation.service";


@Pipe({name: 'translate'})
export class translatePipe implements PipeTransform {
    public translationService: translationService;

constructor(@Inject(translationService) translationService: translationService){
    this.translationService = translationService;
}
transform(value: string) {
    this.translationService.translate(value).subscribe(function(ret){
        return ret;
    });

}
}

Service that sends the request

import {Observable}         from "rxjs/Observable";
import {Observer}           from "rxjs/Observer";

import {Injectable} from "@angular/core";
import {promise} from "selenium-webdriver";
import {Http, Response} from "@angular/http";
import {Constants} from "../../shared/constants";

@Injectable()
export class translationService{
    private _translations: Observable<Object> = null;

    constructor(public http:Http, public constants:Constants){
        this._translateServiceApiUrl = this.constants.APIHost + this.constants.APIPath + this.constants.serviceApiPaths['translations'] + '/it-IT?tags=site';
}

    public getTranslate(): Observable<Object>{
        if (!this._translations || this._translations == null){
            this._translations = this.http
                .get(this._translateServiceApiUrl)
                .map((res:Response)=>res.json() as Object)
        }
        return this._translations;
    }

    public translate(value: string){
        return this.getTranslate();
    }
}

When you try to call Pipe, returns a void, although the console.log shows that the data was received.

fhdhsni
  • 1,529
  • 1
  • 13
  • 21
Ashot
  • 245
  • 6
  • 16
  • 2
    You don't return the subscription from the pipe; just putting a return in the callback won't actually do anything. Just `return this.translationService.translate(value)` and resolve it in the template with the AsyncPipe: `{{ whatever | translate| async }}`. – jonrsharpe Apr 09 '17 at 13:01
  • Thanks @jonrsharpe. Your comment is as good as answer for me. Helped me get un-stuck – checketts Jul 27 '18 at 02:12

0 Answers0