Skip to content

Answer:59 resource signal #1292

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { ChangeDetectionStrategy, Component, model } from '@angular/core';

@Component({
selector: 'app-expandable-card',
Expand Down Expand Up @@ -50,5 +50,5 @@ import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
},
})
export class ExpandableCard {
public isExpanded = signal(false);
public isExpanded = model(false);
}
18 changes: 13 additions & 5 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,9 @@
import { httpResource } from '@angular/common/http';
import {
ChangeDetectionStrategy,
Component,
resource,
ResourceStatus,
signal,
} from '@angular/core';
import { ExpandableCard } from './expandable-card';

Expand All @@ -17,7 +18,7 @@ interface Post {
selector: 'app-page-2',
template: `
page2
<app-expandable-card>
<app-expandable-card [(isExpanded)]="toLoad">
Copy link
Owner

Choose a reason for hiding this comment

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

That's a good solution as well 👍

<div title>Load Post</div>
<div>
@if (postResource.isLoading()) {
Expand All @@ -36,8 +37,15 @@ interface Post {
imports: [ExpandableCard],
})
export class Page2 {
public postResource = httpResource<Post[]>(
'https://jsonplaceholder.typicode.com/posts',
);
toLoad = signal(false);
postResource = resource({
request: () => ({ loading: this.toLoad() }),
loader: async ({ request }) => {
if (!request.loading) return;
const res = await fetch('https://jsonplaceholder.typicode.com/posts');
if (!res.ok) throw new Error('Failed to load posts');
return await res.json();
},
});
protected readonly ResourceStatus = ResourceStatus;
}