Skip to content

Commit 5f8c1b2

Browse files
committed
feat(challenge 59): added lazy loading for posts
1 parent 805b53b commit 5f8c1b2

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

apps/angular/59-content-projection-defer/src/app/expandable-card.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
1+
import {
2+
ChangeDetectionStrategy,
3+
Component,
4+
output,
5+
signal,
6+
} from '@angular/core';
27

38
@Component({
49
selector: 'app-expandable-card',
510
template: `
611
<button
712
class="text-fg-subtle hover:bg-button-secondary-bg-hover active:bg-button-secondary-bg-active focus:outline-button-border-highlight flex w-fit items-center gap-1 py-2 focus:outline focus:outline-2 focus:outline-offset-1"
8-
(click)="isExpanded.set(!isExpanded())"
13+
(click)="expandedChange()"
914
data-cy="expandable-panel-toggle">
1015
@if (isExpanded()) {
1116
<svg
@@ -51,4 +56,10 @@ import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
5156
})
5257
export class ExpandableCard {
5358
public isExpanded = signal(false);
59+
expanded = output<boolean>();
60+
61+
expandedChange = () => {
62+
this.isExpanded.set(!this.isExpanded());
63+
this.expanded.emit(this.isExpanded());
64+
};
5465
}

apps/angular/59-content-projection-defer/src/app/page-2.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
ChangeDetectionStrategy,
44
Component,
55
ResourceStatus,
6+
signal,
67
} from '@angular/core';
78
import { ExpandableCard } from './expandable-card';
89

@@ -17,7 +18,7 @@ interface Post {
1718
selector: 'app-page-2',
1819
template: `
1920
page2
20-
<app-expandable-card>
21+
<app-expandable-card (expanded)="triggerFetch.set($event)">
2122
<div title>Load Post</div>
2223
<div>
2324
@if (postResource.isLoading()) {
@@ -36,8 +37,11 @@ interface Post {
3637
imports: [ExpandableCard],
3738
})
3839
export class Page2 {
39-
public postResource = httpResource<Post[]>(
40-
'https://jsonplaceholder.typicode.com/posts',
40+
protected triggerFetch = signal(false);
41+
public postResource = httpResource<Post[]>(() =>
42+
!this.triggerFetch()
43+
? undefined
44+
: 'https://jsonplaceholder.typicode.com/posts',
4145
);
4246
protected readonly ResourceStatus = ResourceStatus;
4347
}

0 commit comments

Comments
 (0)