Skip to content

Commit 2bc5992

Browse files
author
Your Name
committed
Angular Core Deep Dive
1 parent 1b60d57 commit 2bc5992

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

src/app/app.component.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ import {HighlightedDirective} from './directives/highlighted.directive';
66
import {Observable} from 'rxjs';
77
import {HttpClient, HttpParams} from '@angular/common/http';
88
import {CoursesService} from './services/courses.service';
9+
import {APP_CONFIG, AppConfig, CONFIG_TOKEN} from './config';
910

1011

1112
@Component({
1213
selector: 'app-root',
1314
templateUrl: './app.component.html',
14-
styleUrls: ['./app.component.css'],
15-
providers: [
16-
CoursesService
17-
]
15+
styleUrls: ['./app.component.css']
1816
})
1917
export class AppComponent implements OnInit {
2018

2119
courses$: Observable<Course[]>;
2220

2321
constructor(
24-
private coursesService: CoursesService) {
22+
private coursesService: CoursesService,
23+
@Inject(CONFIG_TOKEN) private config: AppConfig) {
2524

26-
console.log("root component " + this.coursesService.id);
25+
26+
console.log(config);
2727

2828
}
2929

src/app/config.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {InjectionToken} from '@angular/core';
2+
3+
4+
export interface AppConfig {
5+
apiUrl:string;
6+
courseCacheSize:number;
7+
}
8+
9+
10+
export const APP_CONFIG:AppConfig = {
11+
apiUrl: 'http://localhost:9000',
12+
courseCacheSize: 10
13+
}
14+
15+
16+
export const CONFIG_TOKEN =
17+
new InjectionToken<AppConfig>('CONFIG_TOKEN',
18+
{
19+
providedIn: 'root',
20+
factory: () => APP_CONFIG
21+
});

src/app/course-card/course-card.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export class CourseCardComponent implements OnInit {
3939

4040
ngOnInit() {
4141

42-
console.log("coursesService course card" + this.coursesService.id);
4342

4443
}
4544

src/app/services/courses.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
66
let counter = 0;
77

88

9-
@Injectable()
9+
@Injectable({
10+
providedIn: 'root'
11+
})
1012
export class CoursesService {
1113

1214
id:number;

0 commit comments

Comments
 (0)