Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { inject, Pipe, PipeTransform } from '@angular/core';
import { map, Observable } from 'rxjs';
import { CurrencyService } from './currency.service';

@Pipe({
Expand All @@ -9,7 +8,7 @@ import { CurrencyService } from './currency.service';
export class CurrencyPipe implements PipeTransform {
currencyService = inject(CurrencyService);

transform(price: number): Observable<string> {
return this.currencyService.symbol$.pipe(map((s) => `${price}${s}`));
transform(price: number): string {
return `${price}${this.currencyService.symbol()}`;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject, map } from 'rxjs';
import { computed, Injectable, signal } from '@angular/core';

export interface Currency {
name: string;
Expand All @@ -17,14 +16,14 @@ export const currency: Currency[] = [

@Injectable()
export class CurrencyService {
private code = new BehaviorSubject('EUR');
private code = signal<string>('EUR');

readonly code$ = this.code.asObservable();
readonly symbol$ = this.code$.pipe(
map((code) => currency.find((c) => c.code === code)?.symbol ?? code),
);
readonly symbol = computed(() => {
const currentCode = this.code();
return currency.find((c) => c.code === currentCode)?.symbol ?? currentCode;
});

public updateCode(code: string) {
this.code.next(code);
updateCode(newCode: string) {
this.code.set(newCode);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AsyncPipe } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
Expand All @@ -13,11 +12,11 @@ import { Product } from './product.model';
selector: 'tr[product-row]',
template: `
<td>{{ productInfo.name }}</td>
<td>{{ productInfo.priceA | currency | async }}</td>
<td>{{ productInfo.priceB | currency | async }}</td>
<td>{{ productInfo.priceC | currency | async }}</td>
<td>{{ productInfo.priceA | currency }}</td>
<td>{{ productInfo.priceB | currency }}</td>
<td>{{ productInfo.priceC | currency }}</td>
`,
imports: [AsyncPipe, CurrencyPipe],
imports: [CurrencyPipe],
providers: [CurrencyService],
changeDetection: ChangeDetectionStrategy.OnPush,
})
Expand Down
Loading