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-experimental)
18[](https://github.com/TanStack/query/blob/main/LICENSE)
19[](https://bundlephobia.com/package/@tanstack/angular-query-experimental)
20[](https://www.npmjs.com/package/@tanstack/angular-query-experimental)
21β
22# Angular Query
23β
24> IMPORTANT: This library is currently in an experimental stage. This means that breaking changes may happen in minor AND patch releases. Upgrade carefully. If you use this in production while in experimental stage, please lock your version to a patch-level version to avoid unexpected breaking changes.
25β
26Functions for fetching, caching and updating asynchronous data in Angular
27β
28# Documentation
29β
30Visit https://tanstack.com/query/latest/docs/framework/angular/overview
31β
32## Quick Features
33β
34- Transport/protocol/backend agnostic data fetching (REST, GraphQL, promises, whatever!)
35- Auto Caching + Refetching (stale-while-revalidate, Window Refocus, Polling/Realtime)
36- Parallel + Dependent Queries
37- Mutations + Reactive Query Refetching
38- Multi-layer Cache + Automatic Garbage Collection
39- Paginated + Cursor-based Queries
40- Load-More + Infinite Scroll Queries w/ Scroll Recovery
41- Request Cancellation
42- Dedicated Devtools
43β
44# Quick Start
45β
46> The Angular adapter for TanStack Query requires Angular 16 or higher.
47β
481. Install `angular-query`
49β
50```bash
51$ npm i @tanstack/angular-query-experimental
52```
53β
54or
55β
56```bash
57$ pnpm add @tanstack/angular-query-experimental
58```
59β
60or
61β
62```bash
63$ yarn add @tanstack/angular-query-experimental
64```
65β
66or
67β
68```bash
69$ bun add @tanstack/angular-query-experimental
70```
71β
722. Initialize **TanStack Query** by adding **provideTanStackQuery** to your application
73β
74```ts
75import { provideTanStackQuery } from '@tanstack/angular-query-experimental'
76import { QueryClient } from '@tanstack/angular-query-experimental'
77β
78bootstrapApplication(AppComponent, {
79 providers: [provideTanStackQuery(new QueryClient())],
80})
81```
82β
83or in a NgModule-based app
84β
85```ts
86import { provideHttpClient } from '@angular/common/http'
87import {
88 provideTanStackQuery,
89 QueryClient,
90} from '@tanstack/angular-query-experimental'
91β
92@NgModule({
93 declarations: [AppComponent],
94 imports: [BrowserModule],
95 providers: [provideTanStackQuery(new QueryClient())],
96 bootstrap: [AppComponent],
97})
98```
99β
1003. Inject query
101β
102```ts
103import { injectQuery } from '@tanstack/angular-query-experimental'
104import { Component } from '@angular/core'
105β
106@Component({...})
107export class TodosComponent {
108 info = injectQuery(() => ({ queryKey: ['todos'], queryFn: fetchTodoList }))
109}
110```
111β
1124. 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.
113β
114[Open in StackBlitz](https://stackblitz.com/github/TanStack/query/tree/main/examples/angular/basic-angular-router)
115β
116```ts
117@Component({})
118export class PostComponent {
119 #postsService = inject(PostsService)
120 postId = input.required({
121 transform: numberAttribute,
122 })
123β
124 postQuery = injectQuery(() => ({
125 queryKey: ['post', this.postId()],
126 queryFn: () => {
127 return lastValueFrom(this.#postsService.postById$(this.postId()))
128 },
129 }))
130}
131β
132@Injectable({
133 providedIn: 'root',
134})
135export class PostsService {
136 #http = inject(HttpClient)
137β
138 postById$ = (postId: number) =>
139 this.#http.get<Post>(`https://jsonplaceholder.typicode.com/posts/${postId}`)
140}
141β
142export interface Post {
143 id: number
144 title: string
145 body: string
146}
147```
148β
149<!-- -->