11import { TableComponent } from '@angular-challenges/shared/ui' ;
22import { AsyncPipe } from '@angular/common' ;
3- import { ChangeDetectionStrategy , Component , Directive } from '@angular/core' ;
3+ import {
4+ ChangeDetectionStrategy ,
5+ Component ,
6+ Directive ,
7+ inject ,
8+ input ,
9+ } from '@angular/core' ;
410import { CurrencyPipe } from './currency.pipe' ;
511import { CurrencyService } from './currency.service' ;
612import { Product , products } from './product.model' ;
@@ -22,8 +28,30 @@ export class ProductDirective {
2228 }
2329}
2430
31+ @Directive ( {
32+ selector : 'tr[product]' ,
33+ } )
34+ export class TableRowDirective {
35+ private readonly currencyService = inject ( CurrencyService ) ;
36+
37+ product = input < Product > ( ) ;
38+
39+ ngOnInit ( ) {
40+ const product = this . product ( ) ;
41+ if ( product ) {
42+ this . currencyService . patchState ( { code : product . currencyCode } ) ;
43+ }
44+ }
45+ }
46+
2547@Component ( {
26- imports : [ TableComponent , CurrencyPipe , AsyncPipe , ProductDirective ] ,
48+ imports : [
49+ TableComponent ,
50+ CurrencyPipe ,
51+ AsyncPipe ,
52+ ProductDirective ,
53+ TableRowDirective ,
54+ ] ,
2755 providers : [ CurrencyService ] ,
2856 selector : 'app-root' ,
2957 template : `
@@ -38,7 +66,7 @@ export class ProductDirective {
3866 </tr>
3967 </ng-template>
4068 <ng-template #body product let-product>
41- <tr>
69+ <tr [product]="product" >
4270 <td>{{ product.name }}</td>
4371 <td>{{ product.priceA | currency | async }}</td>
4472 <td>{{ product.priceB | currency | async }}</td>
0 commit comments