Skip to content

Answer:16 master dependency injection #1173

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 3 commits 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,6 +1,6 @@
import { TableComponent } from '@angular-challenges/shared/ui';
import { AsyncPipe, NgFor } from '@angular/common';
import { Component, Directive } from '@angular/core';
import { Component, Directive, effect, inject, input } from '@angular/core';
import { CurrencyPipe } from './currency.pipe';
import { CurrencyService } from './currency.service';
import { Product, products } from './product.model';
Expand All @@ -22,8 +22,31 @@ export class ProductDirective {
}
}

@Directive({
selector: '[currencyCode]',
standalone: true,
})
export class CurrencyCodeDirective {
service = inject(CurrencyService);
currencyCode = input<string>('');

constructor() {
effect(() => {
this.service.patchState({ code: this.currencyCode() });
});
}
}

@Component({
imports: [TableComponent, CurrencyPipe, AsyncPipe, NgFor, ProductDirective],
standalone: true,
imports: [
TableComponent,
CurrencyPipe,
AsyncPipe,
NgFor,
ProductDirective,
CurrencyCodeDirective,
],
providers: [CurrencyService],
selector: 'app-root',
template: `
Expand All @@ -36,7 +59,7 @@ export class ProductDirective {
</tr>
</ng-template>
<ng-template #body product let-product>
<tr>
<tr [currencyCode]="product.currencyCode">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect

<td>{{ product.name }}</td>
<td>{{ product.priceA | currency | async }}</td>
<td>{{ product.priceB | currency | async }}</td>
Expand Down
9 changes: 8 additions & 1 deletion apps/angular/39-injection-token/src/app/data.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export const DEFAULT_TIMER = 1000;
import { InjectionToken, ValueProvider } from '@angular/core';

export const TIMER = new InjectionToken<number>('default-timer');

export const getDefaultTimerProvider = (seconds = 1): ValueProvider => ({
provide: TIMER,
useValue: 1000 * seconds,
});
2 changes: 2 additions & 0 deletions apps/angular/39-injection-token/src/app/phone.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Component } from '@angular/core';
import { getDefaultTimerProvider } from './data';
import { TimerContainerComponent } from './timer-container.component';

@Component({
selector: 'app-phone',
imports: [TimerContainerComponent],
providers: [getDefaultTimerProvider(2)],
template: `
<div class="flex gap-2">
Phone Call Timer:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component } from '@angular/core';
import { DEFAULT_TIMER } from './data';
import { Component, inject } from '@angular/core';
import { TIMER } from './data';
import { TimerComponent } from './timer.component';
@Component({
selector: 'timer-container',
Expand All @@ -16,5 +16,5 @@ import { TimerComponent } from './timer.component';
},
})
export class TimerContainerComponent {
timer = DEFAULT_TIMER;
timer = inject(TIMER);
}
9 changes: 5 additions & 4 deletions apps/angular/39-injection-token/src/app/timer.component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Component } from '@angular/core';
import { Component, inject } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { interval } from 'rxjs';
import { DEFAULT_TIMER } from './data';
import { TIMER } from './data';

@Component({
selector: 'timer',
standalone: true,
template: `
Timer running {{ timer() }}
Timer running {{ timerRunning() }}
`,
})
export class TimerComponent {
timer = toSignal(interval(DEFAULT_TIMER));
timer = inject(TIMER);
timerRunning = toSignal(interval(inject(TIMER)));
}
2 changes: 2 additions & 0 deletions apps/angular/39-injection-token/src/app/video.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Component } from '@angular/core';
import { getDefaultTimerProvider } from './data';
import { TimerContainerComponent } from './timer-container.component';

@Component({
selector: 'app-video',
imports: [TimerContainerComponent],
providers: [getDefaultTimerProvider(1)],
template: `
<div class="flex gap-2">
Video Call Timer:
Expand Down