Skip to content

Challenge 59 #1266

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

Closed
wants to merge 2 commits into from
Closed
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,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)="toggleExpanded()"
data-cy="expandable-panel-toggle">
@if (isExpanded()) {
<svg
Expand Down Expand Up @@ -51,4 +56,13 @@ import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
})
export class ExpandableCard {
public isExpanded = signal(false);
expanded = output<void>();

toggleExpanded() {
const newState = !this.isExpanded();
this.isExpanded.set(newState);
if (newState) {
this.expanded.emit();
}
}
}
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
Expand Up @@ -2,7 +2,10 @@ import { httpResource } from '@angular/common/http';
import {
ChangeDetectionStrategy,
Component,
inject,
Injector,
ResourceStatus,
runInInjectionContext,
} from '@angular/core';
import { ExpandableCard } from './expandable-card';

Expand All @@ -17,15 +20,15 @@ interface Post {
selector: 'app-page-2',
template: `
page2
<app-expandable-card>
<app-expandable-card (expanded)="loadPosts()">
<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 @@ -34,10 +37,20 @@ interface Post {
`,
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [ExpandableCard],
standalone: true,
})
export class Page2 {
public postRessource = httpResource<Post[]>(
'https://jsonplaceholder.typicode.com/posts',
);
private readonly injector = inject(Injector);
public postResource: ReturnType<typeof httpResource<Post[]>> | null = null;
protected readonly ResourceStatus = ResourceStatus;

loadPosts() {
if (!this.postResource) {
runInInjectionContext(this.injector, () => {
Copy link
Owner

Choose a reason for hiding this comment

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

using runInInjectionContext is often the sign of a bad desing. You should avoid using it at all cost

this.postResource = httpResource<Post[]>(
'https://jsonplaceholder.typicode.com/posts',
);
});
}
}
}
Loading
Loading