Open History to see the last commit
1<picture>
2 <source
3 media="(prefers-color-scheme: dark)"
4 srcset="https://tanstack.com/api/readme/query.png?framework=angular&theme=dark"
5 />
6 <source
7 media="(prefers-color-scheme: light)"
8 srcset="https://tanstack.com/api/readme/query.png?framework=angular"
9 />
10 <img
11 src="https://tanstack.com/api/readme/query.png?framework=angular"
12 alt="TanStack Angular Query"
13 width="900"
14 />
15</picture>
16β
17[](https://www.npmjs.com/package/@tanstack/angular-query)
18[](https://github.com/TanStack/query/blob/main/LICENSE)
19[](https://bundlephobia.com/package/@tanstack/angular-query)
20[](https://www.npmjs.com/package/@tanstack/angular-query)
21β
22# Angular Query
23β
24Functions for fetching, caching and updating asynchronous data in Angular
25β
26# Documentation
27β
28Visit https://tanstack.com/query/latest/docs/framework/angular/overview
29β
30## Quick Features
31β
32- Transport/protocol/backend agnostic data fetching (REST, GraphQL, promises, whatever!)
33- Auto Caching + Refetching (stale-while-revalidate, Window Refocus, Polling/Realtime)
34- Parallel + Dependent Queries
35- Mutations + Reactive Query Refetching
36- Multi-layer Cache + Automatic Garbage Collection
37- Paginated + Cursor-based Queries
38- Load-More + Infinite Scroll Queries w/ Scroll Recovery
39- Request Cancellation
40- Dedicated Devtools
41β
42# Quick Start
43β
44> The Angular adapter for TanStack Query requires Angular 20.1 or higher.
45β
461. Install `angular-query`
47β
48```bash
49$ npm i @tanstack/angular-query
50```
51β
52or
53β
54```bash
55$ pnpm add @tanstack/angular-query
56```
57β
58or
59β
60```bash
61$ yarn add @tanstack/angular-query
62```
63β
64or
65β
66```bash
67$ bun add @tanstack/angular-query
68```
69β
702. Initialize **TanStack Query** by adding **provideTanStackQuery** to your application
71β
72Pass a factory; Angular runs it once per injector and in an injection context.
73β
74```ts
75import { provideTanStackQuery, QueryClient } from '@tanstack/angular-query'
76β
77bootstrapApplication(AppComponent, {
78 providers: [provideTanStackQuery(() => new QueryClient())],
79})
80```
81β
823. Inject query
83β
84```ts
85import { injectQuery } from '@tanstack/angular-query'
86import { Component } from '@angular/core'
87β
88@Component({...})
89export class TodosComponent {
90 info = injectQuery(() => ({ queryKey: ['todos'], queryFn: fetchTodoList }))
91}
92```
93β
944. If you need to update options on your query dynamically, make sure to pass them as signals. The query will refetch automatically if data for an updated query key is stale or not present.
95β
96[Open in StackBlitz](https://stackblitz.com/github/TanStack/query/tree/main/examples/angular/basic-angular-router)
97β
98```ts
99@Component({})
100export class PostComponent {
101 #postsService = inject(PostsService)
102 postId = input.required({
103 transform: numberAttribute,
104 })
105β
106 postQuery = injectQuery(() => ({
107 queryKey: ['post', this.postId()],
108 queryFn: () => {
109 return lastValueFrom(this.#postsService.postById$(this.postId()))
110 },
111 }))
112}
113β
114@Injectable({
115 providedIn: 'root',
116})
117export class PostsService {
118 #http = inject(HttpClient)
119β
120 postById$ = (postId: number) =>
121 this.#http.get<Post>(`https://jsonplaceholder.typicode.com/posts/${postId}`)
122}
123β
124export interface Post {
125 id: number
126 title: string
127 body: string
128}
129```