Skip to content

Commit 7cb1ec3

Browse files
author
Your Name
committed
Angular Core Deep Dive
1 parent 7508ecb commit 7cb1ec3

File tree

4 files changed

+69
-42
lines changed

4 files changed

+69
-42
lines changed

Diff for: src/app/app.component.html

+10-2
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,24 @@
77

88
<div>
99

10-
<div class="courses" *ngIf="courses$ | async as courses">
10+
<div class="demo">
1111

12-
<course-card highlighted *ngFor="let course of courses"
12+
<button (click)="onEditCourse()">Edit Course</button>
13+
14+
</div>
15+
16+
<div class="courses" *ngIf="courses[0] as course">
17+
18+
<course-card
1319
(courseChanged)="save($event)"
20+
type="beginner"
1421
[course]="course">
1522

1623
<course-image [src]="course.iconUrl"></course-image>
1724

1825
</course-card>
1926

27+
2028
</div>
2129

2230

Diff for: src/app/app.component.ts

+22-25
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,46 @@
1-
import {
2-
AfterViewInit,
3-
Component,
4-
ElementRef,
5-
Inject,
6-
InjectionToken,
7-
OnInit,
8-
Optional,
9-
QueryList,
10-
ViewChild,
11-
ViewChildren
12-
} from '@angular/core';
13-
import {COURSES} from '../db-data';
1+
import {ChangeDetectionStrategy, ChangeDetectorRef, Component, DoCheck, Inject, OnInit} from '@angular/core';
142
import {Course} from './model/course';
15-
import {CourseCardComponent} from './course-card/course-card.component';
16-
import {HighlightedDirective} from './directives/highlighted.directive';
173
import {Observable} from 'rxjs';
18-
import {HttpClient, HttpParams} from '@angular/common/http';
194
import {CoursesService} from './services/courses.service';
20-
import {APP_CONFIG, AppConfig, CONFIG_TOKEN} from './config';
5+
import {AppConfig, CONFIG_TOKEN} from './config';
6+
import {COURSES} from '../db-data';
217

228

239
@Component({
2410
selector: 'app-root',
2511
templateUrl: './app.component.html',
26-
styleUrls: ['./app.component.css'],
27-
providers: [
28-
CoursesService
29-
]
12+
styleUrls: ['./app.component.css']
3013
})
3114
export class AppComponent implements OnInit {
3215

33-
courses$: Observable<Course[]>;
16+
courses: Course[] = COURSES;
17+
3418

3519
constructor(
3620
private coursesService: CoursesService,
3721
@Inject(CONFIG_TOKEN) private config: AppConfig) {
3822

3923

40-
console.log(config);
41-
4224
}
4325

26+
4427
ngOnInit() {
4528

46-
this.courses$ = this.coursesService.loadCourses();
29+
30+
31+
}
32+
33+
onEditCourse() {
34+
35+
const course = this.courses[0];
36+
37+
const newCourse = {
38+
...course,
39+
description: 'ngOnChanges',
40+
cardIndex: 1
41+
};
42+
43+
this.courses[0] = newCourse;
4744

4845
}
4946

Diff for: src/app/course-card/course-card.component.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
<div class="course-description">
1111

12-
Edit Title: <input #courseTitle [value]="course.description">
12+
Edit Title: <input #courseTitle [value]="course.description"
13+
(keyup)="onTitleChanged(courseTitle.value)">
1314

1415
</div>
1516

Diff for: src/app/course-card/course-card.component.ts

+35-14
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
import {
2-
AfterContentInit,
3-
AfterViewInit,
2+
Attribute,
3+
ChangeDetectionStrategy,
4+
ChangeDetectorRef,
45
Component,
5-
ContentChildren,
6-
ElementRef,
7-
EventEmitter, Inject,
8-
Input,
9-
OnInit, Optional,
10-
Output,
11-
QueryList, Self, SkipSelf,
12-
ViewEncapsulation
6+
EventEmitter,
7+
Input, OnChanges,
8+
OnDestroy,
9+
OnInit,
10+
Output
1311
} from '@angular/core';
1412
import {Course} from '../model/course';
15-
import {CourseImageComponent} from '../course-image/course-image.component';
1613
import {CoursesService} from '../services/courses.service';
1714

1815

1916
@Component({
2017
selector: 'course-card',
2118
templateUrl: './course-card.component.html',
22-
styleUrls: ['./course-card.component.css']
19+
styleUrls: ['./course-card.component.css'],
20+
changeDetection: ChangeDetectionStrategy.OnPush
2321
})
24-
export class CourseCardComponent implements OnInit {
22+
export class CourseCardComponent implements OnInit, OnDestroy, OnChanges {
2523

2624
@Input()
2725
course: Course;
@@ -33,12 +31,35 @@ export class CourseCardComponent implements OnInit {
3331
courseEmitter = new EventEmitter<Course>();
3432

3533

36-
constructor(private coursesService: CoursesService) {
34+
constructor(private coursesService: CoursesService,
35+
@Attribute('type') private type: string) {
36+
37+
38+
console.log('constructor', this.course);
3739

3840
}
3941

42+
ngOnChanges(changes) {
43+
44+
console.log("ngOnChanges", changes);
45+
}
46+
4047
ngOnInit() {
4148

49+
console.log("ngOnInit", this.course);
50+
51+
52+
}
53+
54+
ngOnDestroy() {
55+
56+
console.log("ngOnDestroy");
57+
58+
}
59+
60+
onTitleChanged(newTitle:string) {
61+
62+
this.course.description = newTitle;
4263

4364
}
4465

0 commit comments

Comments
 (0)