Skip to content

Commit c1788aa

Browse files
committed
feat(challenge 57): implement content projection in card components
1 parent 543770b commit c1788aa

2 files changed

Lines changed: 27 additions & 15 deletions

File tree

apps/angular/57-content-projection-default/src/app/app.component.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { ChangeDetectionStrategy, Component } from '@angular/core';
2-
import { CardComponent } from './card.component';
2+
import { CardComponent, CardTitleComponent } from './card.component';
33

44
@Component({
5-
imports: [CardComponent],
5+
imports: [CardComponent, CardTitleComponent],
66
selector: 'app-root',
77
template: `
8-
<app-card title="Titre 1" message="Message1" />
9-
<app-card title="Titre 2" />
8+
<app-card message="Message1">
9+
<app-card-title>Titre 1</app-card-title>
10+
<div>Message1</div>
11+
</app-card>
12+
<app-card>
13+
<app-card-title>Titre 2</app-card-title>
14+
</app-card>
1015
`,
1116
host: {
1217
class: 'p-4 block flex flex-col gap-1',
Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1-
import { ChangeDetectionStrategy, Component, input } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
2+
3+
@Component({
4+
selector: 'app-card-title',
5+
template: `
6+
<ng-content></ng-content>
7+
`,
8+
changeDetection: ChangeDetectionStrategy.OnPush,
9+
})
10+
export class CardTitleComponent {}
211

312
@Component({
413
selector: 'app-card',
514
imports: [],
615
template: `
7-
<div>{{ title() }}</div>
8-
@if (message()) {
9-
<div>{{ message() }}</div>
10-
} @else {
11-
<div>Aucun message</div>
12-
}
16+
<div>
17+
<ng-content select="app-card-title"></ng-content>
18+
</div>
19+
20+
<div>
21+
<ng-content>Aucun message</ng-content>
22+
</div>
1323
`,
1424
changeDetection: ChangeDetectionStrategy.OnPush,
1525
host: {
1626
class: 'p-4 border border-grey rounded-sm flex flex-col w-[200px]',
1727
},
1828
})
19-
export class CardComponent {
20-
title = input.required<string>();
21-
message = input<string | undefined>(undefined);
22-
}
29+
export class CardComponent {}

0 commit comments

Comments
 (0)