Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions apps/angular/59-content-projection-defer/src/app/page-2.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { httpResource } from '@angular/common/http';
import {
ChangeDetectionStrategy,
Component,
computed,
resource,
ResourceStatus,
viewChild,
} from '@angular/core';
import { ExpandableCard } from './expandable-card';

Expand All @@ -20,12 +22,12 @@ interface Post {
<app-expandable-card>
<div title>Load Post</div>
<div>
@if (postRessource.isLoading()) {
@if (postResource.isLoading()) {
Loading...
} @else if (postRessource.status() === ResourceStatus.Error) {
} @else if (postResource.status() === ResourceStatus.Error) {
Error...
} @else {
@for (post of postRessource.value(); track post.id) {
@for (post of postResource.value(); track post.id) {
<div>{{ post.title }}</div>
}
}
Expand All @@ -36,8 +38,19 @@ interface Post {
imports: [ExpandableCard],
})
export class Page2 {
public postRessource = httpResource<Post[]>(
'https://jsonplaceholder.typicode.com/posts',
);
protected readonly ResourceStatus = ResourceStatus;
private readonly URL = 'https://jsonplaceholder.typicode.com/posts';

private readonly card = viewChild.required(ExpandableCard);
private readonly isCardOpened = computed(() => this.card().isExpanded());

protected readonly postResource = resource<Post[], { isCardOpened: boolean }>(
{
loader: async ({ request }) =>
request.isCardOpened
? await fetch(this.URL).then((res) => res.json())
: [],
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think how angular designed the API is to return undefined when you don't want to trigger the API call but nice solution.

request: () => ({ isCardOpened: this.isCardOpened() }),
},
);
}