Skip to content

Answer:54 #1283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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,8 @@ 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 {
const symbol = this.currencyService.symbol();
return `${price}${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('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 current = this.code();
return currency.find((c) => c.code === current)?.symbol ?? current;
});

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