1
- import { httpResource } from '@angular/common/http' ;
1
+ import { httpResource , HttpResourceRef } from '@angular/common/http' ;
2
2
import {
3
3
ChangeDetectionStrategy ,
4
4
Component ,
5
+ inject ,
6
+ Injector ,
5
7
ResourceStatus ,
8
+ signal ,
6
9
} from '@angular/core' ;
10
+ import { takeUntilDestroyed , toObservable } from '@angular/core/rxjs-interop' ;
7
11
import { ExpandableCard } from './expandable-card' ;
8
12
9
13
interface Post {
@@ -17,16 +21,18 @@ interface Post {
17
21
selector : 'app-page-2' ,
18
22
template : `
19
23
page2
20
- <app-expandable-card>
24
+ <app-expandable-card (expanded)="triggerFetch.set($event)" >
21
25
<div title>Load Post</div>
22
26
<div>
23
- @if (postRessource.isLoading()) {
24
- Loading...
25
- } @else if (postRessource.status() === ResourceStatus.Error) {
26
- Error...
27
- } @else {
28
- @for (post of postRessource.value(); track post.id) {
29
- <div>{{ post.title }}</div>
27
+ @if (postRessource) {
28
+ @if (postRessource.isLoading()) {
29
+ Loading...
30
+ } @else if (postRessource.status() === ResourceStatus.Error) {
31
+ Error...
32
+ } @else {
33
+ @for (post of postRessource.value(); track post.id) {
34
+ <div>{{ post.title }}</div>
35
+ }
30
36
}
31
37
}
32
38
</div>
@@ -36,8 +42,22 @@ interface Post {
36
42
imports : [ ExpandableCard ] ,
37
43
} )
38
44
export class Page2 {
39
- public postRessource = httpResource < Post [ ] > (
40
- 'https://jsonplaceholder.typicode.com/posts' ,
41
- ) ;
45
+ public postRessource : HttpResourceRef < Post [ ] | undefined > | null = null ;
46
+ protected triggerFetch = signal ( false ) ;
47
+ #injector = inject ( Injector ) ;
48
+ constructor ( ) {
49
+ toObservable ( this . triggerFetch )
50
+ . pipe ( takeUntilDestroyed ( ) )
51
+ . subscribe ( ( trigger ) => {
52
+ if ( trigger ) {
53
+ this . postRessource = httpResource < Post [ ] > (
54
+ 'https://jsonplaceholder.typicode.com/posts' ,
55
+ {
56
+ injector : this . #injector,
57
+ } ,
58
+ ) ;
59
+ }
60
+ } ) ;
61
+ }
42
62
protected readonly ResourceStatus = ResourceStatus ;
43
63
}
0 commit comments