Skip to content

Commit 5697f25

Browse files
committed
feat(Answer:54): solution
1 parent 543770b commit 5697f25

4 files changed

Lines changed: 24 additions & 24 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { inject, Pipe, PipeTransform } from '@angular/core';
2-
import { map, Observable } from 'rxjs';
32
import { CurrencyService } from './currency.service';
43

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

11-
transform(price: number): Observable<string> {
12-
return this.currencyService.symbol$.pipe(map((s) => `${price}${s}`));
10+
transform(price: number): string {
11+
const symbol = this.currencyService.symbol();
12+
return `${price}${symbol}`;
1313
}
1414
}

apps/signal/54-pipe-observable-to-signal/src/app/currency.service.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { Injectable } from '@angular/core';
2-
import { BehaviorSubject, map } from 'rxjs';
1+
import { computed, Injectable, signal } from '@angular/core';
32

43
export interface Currency {
54
name: string;
@@ -17,14 +16,13 @@ export const currency: Currency[] = [
1716

1817
@Injectable()
1918
export class CurrencyService {
20-
private code = new BehaviorSubject('EUR');
19+
private _code = signal('EUR');
2120

22-
readonly code$ = this.code.asObservable();
23-
readonly symbol$ = this.code$.pipe(
24-
map((code) => currency.find((c) => c.code === code)?.symbol ?? code),
21+
readonly symbol = computed(
22+
() => currency.find((c) => c.code === this._code())?.symbol ?? this._code(),
2523
);
2624

2725
public updateCode(code: string) {
28-
this.code.next(code);
26+
this._code.set(code);
2927
}
3028
}
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { AsyncPipe } from '@angular/common';
21
import {
32
ChangeDetectionStrategy,
43
Component,
4+
effect,
55
inject,
6-
Input,
6+
input,
77
} from '@angular/core';
88
import { CurrencyPipe } from './currency.pipe';
99
import { CurrencyService } from './currency.service';
@@ -13,22 +13,23 @@ import { Product } from './product.model';
1313
// eslint-disable-next-line @angular-eslint/component-selector
1414
selector: 'tr[product-row]',
1515
template: `
16-
<td>{{ productInfo.name }}</td>
17-
<td>{{ productInfo.priceA | currency | async }}</td>
18-
<td>{{ productInfo.priceB | currency | async }}</td>
19-
<td>{{ productInfo.priceC | currency | async }}</td>
16+
<td>{{ productInfo().name }}</td>
17+
<td>{{ productInfo().priceA | currency }}</td>
18+
<td>{{ productInfo().priceB | currency }}</td>
19+
<td>{{ productInfo().priceC | currency }}</td>
2020
`,
21-
imports: [AsyncPipe, CurrencyPipe],
21+
imports: [CurrencyPipe],
2222
providers: [CurrencyService],
2323
changeDetection: ChangeDetectionStrategy.OnPush,
2424
})
2525
export class ProductRowComponent {
26-
protected productInfo!: Product;
26+
productInfo = input.required<Product>({ alias: 'product' });
27+
currencyService = inject(CurrencyService);
2728

28-
@Input({ required: true }) set product(product: Product) {
29-
this.currencyService.updateCode(product.currencyCode);
30-
this.productInfo = product;
29+
constructor() {
30+
effect(() => {
31+
const product = this.productInfo();
32+
this.currencyService.updateCode(product.currencyCode);
33+
});
3134
}
32-
33-
currencyService = inject(CurrencyService);
3435
}

libs/shared/ui/src/lib/table.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { NgTemplateOutlet } from '@angular/common';
12
import { Component, contentChild, input, TemplateRef } from '@angular/core';
23

34
@Component({
45
// eslint-disable-next-line @angular-eslint/component-selector
56
selector: 'table',
6-
imports: [],
7+
imports: [NgTemplateOutlet],
78
template: `
89
<thead>
910
<ng-container *ngTemplateOutlet="headerTemplate()"></ng-container>

0 commit comments

Comments
 (0)