Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Answer: 59 #1264

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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,11 +1,16 @@
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import {
ChangeDetectionStrategy,
Component,
output,
signal,
} from '@angular/core';

@Component({
selector: 'app-expandable-card',
template: `
<button
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"
(click)="isExpanded.set(!isExpanded())"
(click)="toggleExpansion()"
data-cy="expandable-panel-toggle">
@if (isExpanded()) {
<svg
@@ -51,4 +56,11 @@ import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
})
export class ExpandableCard {
public isExpanded = signal(false);

readonly expanded = output<boolean>();

toggleExpansion(): void {
this.isExpanded.update((isExpanded) => !isExpanded);
this.expanded.emit(this.isExpanded());
}
}
17 changes: 14 additions & 3 deletions apps/angular/59-content-projection-defer/src/app/page-2.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import {
ChangeDetectionStrategy,
Component,
ResourceStatus,
signal,
} from '@angular/core';
import { ExpandableCard } from './expandable-card';

@@ -17,7 +18,7 @@ interface Post {
selector: 'app-page-2',
template: `
page2
<app-expandable-card>
<app-expandable-card (expanded)="onExpansion($event)">
<div title>Load Post</div>
<div>
@if (postRessource.isLoading()) {
@@ -36,8 +37,18 @@ interface Post {
imports: [ExpandableCard],
})
export class Page2 {
public postRessource = httpResource<Post[]>(
'https://jsonplaceholder.typicode.com/posts',
// Set this signal to true to start fetching the posts
private readonly $loader = signal(false);

// Fetch posts from the API
public postRessource = httpResource<Post[]>(() =>
this.$loader() ? 'https://jsonplaceholder.typicode.com/posts' : undefined,
);
protected readonly ResourceStatus = ResourceStatus;

onExpansion(isExpanded: boolean) {
if (isExpanded && !this.postRessource.hasValue()) {
this.$loader.set(true);
}
}
}
1,200 changes: 579 additions & 621 deletions package-lock.json

Large diffs are not rendered by default.