Skip to content

Commit 503196e

Browse files
committed
feat(): migrate challege 4
1 parent 8ce5402 commit 503196e

File tree

3 files changed

+18
-26
lines changed

3 files changed

+18
-26
lines changed

apps/angular/4-typed-context-outlet/src/app/app.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { NgTemplateOutlet } from '@angular/common';
21
import { ChangeDetectionStrategy, Component } from '@angular/core';
32
import { ListComponent } from './list.component';
43
import { PersonComponent } from './person.component';
54

65
@Component({
7-
imports: [NgTemplateOutlet, PersonComponent, ListComponent],
6+
imports: [PersonComponent, ListComponent],
87
selector: 'app-root',
98
template: `
109
<person [person]="person">
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
import { CommonModule } from '@angular/common';
1+
import { NgTemplateOutlet } from '@angular/common';
22
import {
33
ChangeDetectionStrategy,
44
Component,
5-
ContentChild,
6-
Input,
5+
contentChild,
6+
input,
77
TemplateRef,
88
} from '@angular/core';
99

1010
@Component({
1111
selector: 'list',
12-
imports: [CommonModule],
1312
template: `
14-
<div *ngFor="let item of list; index as i">
13+
@for (item of list(); track $index) {
1514
<ng-container
1615
*ngTemplateOutlet="
17-
listTemplateRef || emptyRef;
18-
context: { $implicit: item, appList: item, index: i }
19-
"></ng-container>
20-
</div>
16+
listTemplateRef() || emptyRef;
17+
context: { $implicit: item, appList: item, index: $index }
18+
" />
19+
}
2120
2221
<ng-template #emptyRef>No Template</ng-template>
2322
`,
2423
changeDetection: ChangeDetectionStrategy.OnPush,
24+
imports: [NgTemplateOutlet],
2525
})
2626
export class ListComponent<TItem extends object> {
27-
@Input() list!: TItem[];
27+
list = input.required<TItem[]>();
2828

29-
@ContentChild('listRef', { read: TemplateRef })
30-
listTemplateRef!: TemplateRef<unknown>;
29+
listTemplateRef = contentChild('listRef', { read: TemplateRef });
3130
}
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
import { NgTemplateOutlet } from '@angular/common';
2-
import { Component, ContentChild, Input, TemplateRef } from '@angular/core';
3-
4-
interface Person {
5-
name: string;
6-
age: number;
7-
}
2+
import { Component, contentChild, input, TemplateRef } from '@angular/core';
83

94
@Component({
105
imports: [NgTemplateOutlet],
116
selector: 'person',
127
template: `
138
<ng-container
149
*ngTemplateOutlet="
15-
personTemplateRef || emptyRef;
16-
context: { $implicit: person.name, age: person.age }
17-
"></ng-container>
10+
personTemplateRef() || emptyRef;
11+
context: { $implicit: person().name, age: person().age }
12+
" />
1813
1914
<ng-template #emptyRef>No Template</ng-template>
2015
`,
2116
})
2217
export class PersonComponent {
23-
@Input() person!: Person;
18+
person = input.required<{ name: string; age: number }>();
2419

25-
@ContentChild('#personRef', { read: TemplateRef })
26-
personTemplateRef!: TemplateRef<unknown>;
20+
personTemplateRef = contentChild('personRef', { read: TemplateRef });
2721
}

0 commit comments

Comments
 (0)