Skip to content

Commit 38dfd6a

Browse files
committed
feat: 39-injection-token
1 parent 8f40f41 commit 38dfd6a

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1+
import { InjectionToken } from '@angular/core';
2+
13
export const DEFAULT_TIMER = 1000;
4+
5+
export const TIMER_TOKEN = new InjectionToken<number>('TIMER_TOKEN', {
6+
providedIn: 'root',
7+
factory: () => DEFAULT_TIMER,
8+
});

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';
2+
import { TIMER_TOKEN } from './data';
23
import { TimerContainerComponent } from './timer-container.component';
34

45
@Component({
56
selector: 'app-phone',
67
imports: [TimerContainerComponent],
8+
providers: [{ provide: TIMER_TOKEN, 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_TOKEN } from './data';
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_TOKEN);
2020
}
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_TOKEN } from './data';
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_TOKEN)));
1414
}

0 commit comments

Comments
 (0)