带有Observable

时间:2017-04-09 12:55:51

标签: angular pipe observable rxjs5

我正在尝试创建Pipe 哪个应该向服务器发送请求,等待答案,然后返回结果

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;
    });

}
}

发送请求的服务

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();
    }
}

当您尝试调用Pipe时,返回一个void,尽管console.log显示已收到数据。

0 个答案:

没有答案