diff --git a/apps/signal/54-pipe-observable-to-signal/src/app/currency.pipe.ts b/apps/signal/54-pipe-observable-to-signal/src/app/currency.pipe.ts index 411243b81..66b030b89 100644 --- a/apps/signal/54-pipe-observable-to-signal/src/app/currency.pipe.ts +++ b/apps/signal/54-pipe-observable-to-signal/src/app/currency.pipe.ts @@ -1,5 +1,4 @@ import { inject, Pipe, PipeTransform } from '@angular/core'; -import { map, Observable } from 'rxjs'; import { CurrencyService } from './currency.service'; @Pipe({ @@ -8,7 +7,9 @@ import { CurrencyService } from './currency.service'; export class CurrencyPipe implements PipeTransform { currencyService = inject(CurrencyService); - transform(price: number): Observable { - return this.currencyService.symbol$.pipe(map((s) => `${price}${s}`)); + transform(price: number, currencyCode: string): string { + const symbol = this.currencyService.getSymbol(currencyCode); + + return `${price}${symbol}`; } } diff --git a/apps/signal/54-pipe-observable-to-signal/src/app/currency.service.ts b/apps/signal/54-pipe-observable-to-signal/src/app/currency.service.ts index 8ddf570bf..7432edeba 100644 --- a/apps/signal/54-pipe-observable-to-signal/src/app/currency.service.ts +++ b/apps/signal/54-pipe-observable-to-signal/src/app/currency.service.ts @@ -1,5 +1,4 @@ import { Injectable } from '@angular/core'; -import { BehaviorSubject, map } from 'rxjs'; export interface Currency { name: string; @@ -17,14 +16,7 @@ export const currency: Currency[] = [ @Injectable() export class CurrencyService { - private code = new BehaviorSubject('EUR'); - - readonly code$ = this.code.asObservable(); - readonly symbol$ = this.code$.pipe( - map((code) => currency.find((c) => c.code === code)?.symbol ?? code), - ); - - public updateCode(code: string) { - this.code.next(code); + getSymbol(code: string): string { + return currency.find((c) => c.code === code)?.symbol ?? code; } } diff --git a/apps/signal/54-pipe-observable-to-signal/src/app/product-row.component.ts b/apps/signal/54-pipe-observable-to-signal/src/app/product-row.component.ts index d39777971..d6adf7e14 100644 --- a/apps/signal/54-pipe-observable-to-signal/src/app/product-row.component.ts +++ b/apps/signal/54-pipe-observable-to-signal/src/app/product-row.component.ts @@ -1,9 +1,8 @@ -import { AsyncPipe } from '@angular/common'; import { ChangeDetectionStrategy, Component, - inject, - Input, + computed, + input, } from '@angular/core'; import { CurrencyPipe } from './currency.pipe'; import { CurrencyService } from './currency.service'; @@ -13,22 +12,17 @@ import { Product } from './product.model'; // eslint-disable-next-line @angular-eslint/component-selector selector: 'tr[product-row]', template: ` - {{ productInfo.name }} - {{ productInfo.priceA | currency | async }} - {{ productInfo.priceB | currency | async }} - {{ productInfo.priceC | currency | async }} + {{ product().name }} + {{ product().priceA | currency: currencyCode() }} + {{ product().priceB | currency: currencyCode() }} + {{ product().priceC | currency: currencyCode() }} `, - imports: [AsyncPipe, CurrencyPipe], + imports: [CurrencyPipe], providers: [CurrencyService], changeDetection: ChangeDetectionStrategy.OnPush, }) export class ProductRowComponent { - protected productInfo!: Product; + product = input.required(); - @Input({ required: true }) set product(product: Product) { - this.currencyService.updateCode(product.currencyCode); - this.productInfo = product; - } - - currencyService = inject(CurrencyService); + currencyCode = computed(() => this.product().currencyCode); } diff --git a/libs/shared/ui/src/lib/table.component.ts b/libs/shared/ui/src/lib/table.component.ts index 7b5159326..1c38c6696 100644 --- a/libs/shared/ui/src/lib/table.component.ts +++ b/libs/shared/ui/src/lib/table.component.ts @@ -1,9 +1,10 @@ +import { NgTemplateOutlet } from '@angular/common'; import { Component, contentChild, input, TemplateRef } from '@angular/core'; @Component({ // eslint-disable-next-line @angular-eslint/component-selector selector: 'table', - imports: [], + imports: [NgTemplateOutlet], template: `