File tree Expand file tree Collapse file tree 3 files changed +11
-15
lines changed
apps/signal/54-pipe-observable-to-signal/src/app Expand file tree Collapse file tree 3 files changed +11
-15
lines changed Original file line number Diff line number Diff line change 11import { inject , Pipe , PipeTransform } from '@angular/core' ;
2- import { map , Observable } from 'rxjs' ;
32import { CurrencyService } from './currency.service' ;
43
54@Pipe ( {
@@ -9,7 +8,7 @@ import { CurrencyService } from './currency.service';
98export 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}
Original file line number Diff line number Diff line change 1- import { Injectable } from '@angular/core' ;
2- import { BehaviorSubject , map } from 'rxjs' ;
1+ import { computed , Injectable , signal } from '@angular/core' ;
32
43export interface Currency {
54 name : string ;
@@ -17,14 +16,13 @@ export const currency: Currency[] = [
1716
1817@Injectable ( )
1918export 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}
Original file line number Diff line number Diff line change 1- import { AsyncPipe } from '@angular/common' ;
21import {
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} )
You can’t perform that action at this time.
0 commit comments