最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use HTTP calls in pure pipe in angular 5 - Stack Overflow

programmeradmin1浏览0评论

I am creating a pipe to convert one currency value to another. I am making HTTP call to convert value.

@Pipe({
    name: "currencyConverter"
})
export class CurrencyConverterPipe implements PipeTransform {

    transform(value: number, currencyPair: string): number {
        let sourceCurrency = currencyPair.split(":")[0];
        let destinationCurrency = currencyPair.split(":")[1];
        this.currencyConverterService.convert(sourceCurrency, destinationCurrency).subscribe(successData => {
            return successData['fiatValue'] * value;
        }, errorData => {
            console.error(errorData);
        })
    }
}

In HTML

<p> {{ value | currencyConverter:'BTC:USD'}}</p>

I am unable to see any value in UI. When i make my pipe impure, Then it works but it is making many HTTP calls to server. Is there any way to make HTTP call without using pure: false in pipe

I am creating a pipe to convert one currency value to another. I am making HTTP call to convert value.

@Pipe({
    name: "currencyConverter"
})
export class CurrencyConverterPipe implements PipeTransform {

    transform(value: number, currencyPair: string): number {
        let sourceCurrency = currencyPair.split(":")[0];
        let destinationCurrency = currencyPair.split(":")[1];
        this.currencyConverterService.convert(sourceCurrency, destinationCurrency).subscribe(successData => {
            return successData['fiatValue'] * value;
        }, errorData => {
            console.error(errorData);
        })
    }
}

In HTML

<p> {{ value | currencyConverter:'BTC:USD'}}</p>

I am unable to see any value in UI. When i make my pipe impure, Then it works but it is making many HTTP calls to server. Is there any way to make HTTP call without using pure: false in pipe

Share Improve this question edited Feb 21, 2018 at 6:33 Ajit Soman asked Feb 21, 2018 at 6:17 Ajit SomanAjit Soman 4,0693 gold badges25 silver badges42 bronze badges 2
  • 1 Return the observable and apply the async pipe on trmplate – Jota.Toledo Commented Feb 21, 2018 at 6:56
  • @Jota.Toledo A cleaner solution is to extend async, since there's no benefit from using them separately. – Estus Flask Commented Feb 21, 2018 at 7:22
Add a ment  | 

1 Answer 1

Reset to default 9

This pipe isn't pure by design.

In order to not cause multiple requests, it should return same observable that won't do additional requests until input is changed, something like:

@Pipe({
  name: "currencyConverter",
  pure: false
})
export class CurrencyConverterPipe  {
  private sourceCurrency;
  private destinationCurrency;
  private valueSubject = new Subject();
  private value$ = this.valueSubject.asObservable().distinctUntilChanged()
  .switchMap(value => {
     return this.currencyConverter
     .convert(sourceCurrency, destinationCurrency))
     .map((data) => data * value);
  });

  constructor(private currencyConverter: ...) {}

  transform(value: number, currencyPair: string): number {
    this.sourceCurrency = currencyPair.split(":")[0];
    this.destinationCurrency = currencyPair.split(":")[1];
    this.valueSubject.next(value);
    return this.value$;
  }
}

Since it is supposed to be used only in conjunction with async pipe, it makes sense to join them together and extend AsyncPipe:

@Pipe({
  name: "currencyConverterAsync",
  pure: false
})
export class CurrencyConverterPipe extends AsyncPipe {
  private sourceCurrency;
  private destinationCurrency;
  private valueSubject = new Subject();
  private value$ = this.valueSubject.asObservable().distinctUntilChanged()
  .switchMap(value => {
     return this.currencyConverter
     .convert(sourceCurrency, destinationCurrency))
     .map((data) => data * value);
  });

  constructor(cdRef: ChangeDetectorRef, private currencyConverter: ...) {
    super(cdRef);
  }

  transform(value: number, currencyPair: string): number {
    this.sourceCurrency = currencyPair.split(":")[0];
    this.destinationCurrency = currencyPair.split(":")[1];
    this.valueSubject.next(value);

    return super.transform(this.value$);
  }
}
发布评论

评论列表(0)

  1. 暂无评论