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