Skip to content

Commit 418d298

Browse files
committed
feat(challenge 16): add TableRowDirective
1 parent 543770b commit 418d298

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

apps/angular/16-master-dependency-injection/src/app/app.component.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { TableComponent } from '@angular-challenges/shared/ui';
22
import { 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';
410
import { CurrencyPipe } from './currency.pipe';
511
import { CurrencyService } from './currency.service';
612
import { 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>

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)