Open History to see the last commit
1---
2id: queries
3title: Queries
4ref: docs/framework/react/guides/queries.md
5replace:
6 {
7 'React': 'Angular',
8 'react-query': 'angular-query',
9 'promise': 'promise or observable',
10 'custom hooks': 'services',
11 'the `useQuery` hook': '`injectQuery`',
12 '`useQuery`': '`injectQuery`',
13 "TypeScript will also narrow the type of data correctly if you've checked for pending and error before accessing it.": 'TypeScript will only narrow the type when checking boolean signals such as `isPending` and `isError`.',
14 }
15---
16β
17[//]: # 'Example'
18β
19```ts
20import { injectQuery } from '@tanstack/angular-query-experimental'
21β
22export class TodosComponent {
23 info = injectQuery(() => ({ queryKey: ['todos'], queryFn: fetchTodoList }))
24}
25```
26β
27[//]: # 'Example'
28[//]: # 'Example2'
29β
30```ts
31result = injectQuery(() => ({ queryKey: ['todos'], queryFn: fetchTodoList }))
32```
33β
34[//]: # 'Example2'
35[//]: # 'Example3'
36β
37```angular-ts
38@Component({
39 selector: 'todos',
40 standalone: true,
41 template: `
42 @if (todos.isPending()) {
43 <span>Loading...</span>
44 } @else if (todos.isError()) {
45 <span>Error: {{ todos.error()?.message }}</span>
46 } @else {
47 <!-- We can assume by this point that status === 'success' -->
48 @for (todo of todos.data(); track todo.id) {
49 <li>{{ todo.title }}</li>
50 } @empty {
51 <li>No todos found</li>
52 }
53 }
54 `,
55})
56export class PostsComponent {
57 todos = injectQuery(() => ({
58 queryKey: ['todos'],
59 queryFn: fetchTodoList,
60 }))
61}
62```
63β
64[//]: # 'Example3'
65β
66If booleans aren't your thing, you can always use the `status` state as well:
67β
68[//]: # 'Example4'
69β
70```angular-ts
71@Component({
72 selector: 'todos',
73 standalone: true,
74 template: `
75 @switch (todos.status()) {
76 @case ('pending') {
77 <span>Loading...</span>
78 }
79 @case ('error') {
80 <span>Error: {{ todos.error()?.message }}</span>
81 }
82 <!-- also status === 'success', but "else" logic works, too -->
83 @default {
84 <ul>
85 @for (todo of todos.data(); track todo.id) {
86 <li>{{ todo.title }}</li>
87 } @empty {
88 <li>No todos found</li>
89 }
90 </ul>
91 }
92 }
93 `,
94})
95class TodosComponent {}
96```
97β
98[//]: # 'Example4'
99[//]: # 'Materials'
100[//]: # 'Materials'