Skip to content

Commit 761f4f6

Browse files
committed
feat(challenge 39): implement injection token for timer configuration
1 parent 543770b commit 761f4f6

4 files changed

Lines changed: 15 additions & 6 deletions

File tree

apps/angular/39-injection-token/src/app/phone.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { Component } from '@angular/core';
22
import { TimerContainerComponent } from './timer-container.component';
3+
import { TIMER } from './timer-provider';
34

45
@Component({
56
selector: 'app-phone',
67
imports: [TimerContainerComponent],
8+
providers: [{ provide: TIMER, useValue: 2000 }],
79
template: `
810
<div class="flex gap-2">
911
Phone Call Timer:

apps/angular/39-injection-token/src/app/timer-container.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Component } from '@angular/core';
2-
import { DEFAULT_TIMER } from './data';
1+
import { Component, inject } from '@angular/core';
2+
import { TIMER } from './timer-provider';
33
import { TimerComponent } from './timer.component';
44
@Component({
55
selector: 'timer-container',
@@ -16,5 +16,5 @@ import { TimerComponent } from './timer.component';
1616
},
1717
})
1818
export class TimerContainerComponent {
19-
timer = DEFAULT_TIMER;
19+
timer = inject(TIMER);
2020
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { InjectionToken } from '@angular/core';
2+
import { DEFAULT_TIMER } from './data';
3+
4+
export const TIMER = new InjectionToken<number>('timer', {
5+
providedIn: 'root',
6+
factory: () => DEFAULT_TIMER,
7+
});
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Component } from '@angular/core';
1+
import { Component, inject } from '@angular/core';
22
import { toSignal } from '@angular/core/rxjs-interop';
33
import { interval } from 'rxjs';
4-
import { DEFAULT_TIMER } from './data';
4+
import { TIMER } from './timer-provider';
55

66
@Component({
77
selector: 'timer',
@@ -10,5 +10,5 @@ import { DEFAULT_TIMER } from './data';
1010
`,
1111
})
1212
export class TimerComponent {
13-
timer = toSignal(interval(DEFAULT_TIMER));
13+
timer = toSignal(interval(inject(TIMER)));
1414
}

0 commit comments

Comments
 (0)