10000 fix(54): pipe observable to signal by tarashevchuk0401 · Pull Request #1231 · tomalaforge/angular-challenges · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(54): pipe observable to signal #1231

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 { Injectable, signal } from '@angular/core';

export interface Currency {
name: string;
Expand All @@ -17,14 +16,11 @@ 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),
);
private code = signal('EUR');
symbol$ = '';

public updateCode(code: string) {
this.code.next(code);
this.code.set(code);
this.symbol$ = currency.find((c) => c.code === this.code())?.symbol ?? code;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ 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],
providers: [CurrencyService],
Expand Down
0