Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Angular State Management


State management organizes how data changes over time.


State Management Essentials

  • Local first: Start with component signals; promote to a service (store) only when sharing is needed.
  • Signals: Use signal() for local and service-backed state; derive with computed().
  • Interop: Use RxJS only when stream semantics are required; keep global state minimal.
import { Injectable, signal, computed, inject } from '@angular/core';

@Injectable({ providedIn: 'root' })
class CounterStore {
  count = signal(0);
  double = computed(() => this.count() * 2);
  inc() { this.count.update(n => n + 1); }
}

// Component: const store = inject(CounterStore);

Notes:


Service-backed Signals (Store)

Lift state into a service to share across components.

It improves reuse.

import { Injectable, signal, inject } from '@angular/core';

@Injectable({ providedIn: 'root' })
class CounterStore {
  count = signal(0);
  inc() { this.count.update(n => n + 1); }
}

// Component usage
class App { store = inject(CounterStore); }

Example

import { bootstrapApplication } from '@angular/platform-browser';
import { Component, Injectable, signal, inject } from '@angular/core';

@Injectable({ providedIn: 'root' })
class CounterStore {
  count = signal(0);
  inc() { this.count.update(n => n + 1); }
}

@Component({
  selector: 'app-root',
  standalone: true,
  template: `
    <h3>Service with Signals</h3>
    <p>Count: {{ store.count() }}</p>
    <button (click)="store.inc()">Increment</button>
  `
})
class App { store = inject(CounterStore); }

bootstrapApplication(App);
<app-root></app-root>

Run Example »

Example explained

  • CounterStore: A service holding a signal and an update method (inc()).
  • inject(CounterStore): Retrieves the store in the component (no constructor required).
  • store.count(): Read the current value in the template; clicking the button calls store.inc().

Design tips:

  • Keep a single source of truth per feature in a service; inject where needed.
  • Expose methods for updates; avoid mutating state from components directly.
  • Derive values with computed signals; keep side effects (like persistence) in the service.
  • Bridge to streams only when needed (e.g., to interop with RxJS-based APIs).

REMOVE ADS


Local vs Global State

  • Keep most state local to components to reduce coupling.
  • Promote to a shared service only when multiple features need it.
  • Scope providers at the feature/route level when isolation is desired.
import { provideRouter, Routes } from '@angular/router';

const routes: Routes = [
  { path: '', component: Home, providers: [CounterStore] }
];

bootstrapApplication(App, { providers: [provideRouter(routes)] });

Guidelines:

  • Promote state when multiple routes/components need the same data or when caching improves UX.
  • Separate UI state (filters, dialogs) from server/cache state; manage them independently.
  • Initialize lazily on first use and consider reset points (e.g., on logout).


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.