Skip to content

Commit 7349de5

Browse files
committed
refactor: use signal
1 parent c33b336 commit 7349de5

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed
Lines changed: 2 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({
@@ -9,7 +8,7 @@ import { CurrencyService } from './currency.service';
98
export class CurrencyPipe implements PipeTransform {
109
currencyService = inject(CurrencyService);
1110

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

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+
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
}

apps/signal/54-pipe-observable-to-signal/src/app/product-row.component.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { AsyncPipe } from '@angular/common';
21
import {
32
ChangeDetectionStrategy,
43
Component,
@@ -13,11 +12,11 @@ import { Product } from './product.model';
1312
selector: 'tr[product-row]',
1413
template: `
1514
<td>{{ productInfo.name }}</td>
16-
<td>{{ productInfo.priceA | currency | async }}</td>
17-
<td>{{ productInfo.priceB | currency | async }}</td>
18-
<td>{{ productInfo.priceC | currency | async }}</td>
15+
<td>{{ productInfo.priceA | currency }}</td>
16+
<td>{{ productInfo.priceB | currency }}</td>
17+
<td>{{ productInfo.priceC | currency }}</td>
1918
`,
20-
imports: [AsyncPipe, CurrencyPipe],
19+
imports: [CurrencyPipe],
2120
providers: [CurrencyService],
2221
changeDetection: ChangeDetectionStrategy.OnPush,
2322
})

0 commit comments

Comments
 (0)