小编Ran*_*obi的帖子

使用 Observable 或 Promise 在 Angular 6 中转换管道

我试图在 angular 6 中设置管道,使用服务将文本转换为其他(返回可观察的方法)

我尝试了以下代码,但我需要返回一个字符串而不是 Promise

管道:

import { Pipe, PipeTransform } from '@angular/core';
import { TimeZoneService, TimeZone } from '../services/Global/timezone.service';
//import { resolve } from 'dns';
import { reject } from 'q';
import { Observable } from 'rxjs';

@Pipe({
  name: 'utcToText'
})
export class UtcToTextPipe implements PipeTransform {

  private timezoneLst: TimeZone[] = [];

  constructor(private _timezoneSvc : TimeZoneService) {}

  async transform(timezone: any, args?: any){
    this.timezoneLst = await this._timezoneSvc.getTimeZonesLst().toPromise();
     return this.timezoneLst.find(x => x.utc.indexOf(timezone) > -1).text;

}
}
Run Code Online (Sandbox Code Playgroud)

html:

<span>{{subscription.time_zone |  utcToText}</span> …
Run Code Online (Sandbox Code Playgroud)

promise observable typescript angular angular6

3
推荐指数
1
解决办法
7271
查看次数

标签 统计

angular ×1

angular6 ×1

observable ×1

promise ×1

typescript ×1