Skip to content
Angular · 7 min read

Angular NGXS CRUD example

By Dapton Technologies·Updated Nov 2026

Build an in-memory todo editor with standalone Angular, NGXS actions and selectors. Add, read, rename, complete and delete items without a fake persistence API.

This small application demonstrates create, read, update and delete operations in application state. It uses Angular 22 and NGXS 22, with a standalone component and a signal selector. Data lives in memory and resets on reload. There is no backend, authentication or claim of production readiness.

1. Create a standalone application

Use a Node.js version supported by Angular 22. Create a client-rendered CLI project, then add NGXS. The example uses the CLI’s short app.ts filename convention.

Terminal
npx @angular/cli@22.2.7 new todo-demo --defaults --style=css --ssr=false
cd todo-demo
npm install @ngxs/store@22.0.0

2. Define the state and actions

Each todo has a required identifier. New items receive a browser-generated UUID, rather than reusing an identifier from a mock API. Rename changes the title while preserving completion. State updates create new arrays and objects.

src/app/todos.state.ts
import { Injectable } from '@angular/core';
import { Action, Selector, State, StateContext } from '@ngxs/store';

export interface Todo {
  id: string;
  title: string;
  completed: boolean;
}
interface TodoModel { items: Todo[]; }
export class AddTodo {
  static readonly type = '[Todos] Add';
  constructor(public title: string) {}
}
export class RenameTodo {
  static readonly type = '[Todos] Rename';
  constructor(public id: string, public title: string) {}
}
export class ToggleTodo {
  static readonly type = '[Todos] Toggle';
  constructor(public id: string) {}
}
export class DeleteTodo {
  static readonly type = '[Todos] Delete';
  constructor(public id: string) {}
}

@State<TodoModel>({ name: 'todos', defaults: { items: [] } })
@Injectable()
export class TodosState {
  @Selector()
  static items(state: TodoModel): Todo[] { return state.items; }

  @Action(AddTodo)
  add(ctx: StateContext<TodoModel>, action: AddTodo): void {
    const title = action.title.trim();
    if (!title) return;
    ctx.patchState({ items: [...ctx.getState().items, {
      id: crypto.randomUUID(), title, completed: false
    }] });
  }

  @Action(RenameTodo)
  rename(ctx: StateContext<TodoModel>, action: RenameTodo): void {
    const title = action.title.trim();
    if (!title) return;
    ctx.patchState({ items: ctx.getState().items.map(todo =>
      todo.id === action.id ? { ...todo, title } : todo
    ) });
  }

  @Action(ToggleTodo)
  toggle(ctx: StateContext<TodoModel>, action: ToggleTodo): void {
    ctx.patchState({ items: ctx.getState().items.map(todo =>
      todo.id === action.id ? { ...todo, completed: !todo.completed } : todo
    ) });
  }

  @Action(DeleteTodo)
  remove(ctx: StateContext<TodoModel>, action: DeleteTodo): void {
    ctx.patchState({ items: ctx.getState().items.filter(todo =>
      todo.id !== action.id
    ) });
  }
}

3. Connect a component

Replace the generated root component with this editor. The selector supplies the list; actions handle changes. The form clears after a successful dispatch. Deleting the item being edited also clears the editor.

src/app/app.ts
import { Component, inject } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Store } from '@ngxs/store';
import {
  AddTodo, DeleteTodo, RenameTodo, Todo, TodosState, ToggleTodo
} from './todos.state';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [FormsModule],
  template: `
    <h1>Todos</h1>
    <form (ngSubmit)="save()">
      <label>Title <input name="title" [(ngModel)]="title" required /></label>
      <button type="submit" [disabled]="!title.trim()">
        {{ editingId === null ? 'Add' : 'Save' }}
      </button>
      @if (editingId !== null) {
        <button type="button" (click)="cancel()">Cancel</button>
      }
    </form>
    <ul>
      @for (todo of todos(); track todo.id) {
        <li>
          <label>
            <input type="checkbox" [checked]="todo.completed"
              (change)="toggle(todo.id)" />
            {{ todo.title }}
          </label>
          <button type="button" (click)="edit(todo)">Edit</button>
          <button type="button" (click)="remove(todo.id)">Delete</button>
        </li>
      } @empty { <li>No todos yet.</li> }
    </ul>
  `
})
export class App {
  private store = inject(Store);
  readonly todos = this.store.selectSignal(TodosState.items);
  title = '';
  editingId: string | null = null;

  save(): void {
    const title = this.title.trim();
    if (!title) return;
    const action = this.editingId === null
      ? new AddTodo(title) : new RenameTodo(this.editingId, title);
    this.store.dispatch(action).subscribe(() => this.cancel());
  }
  edit(todo: Todo): void {
    this.editingId = todo.id;
    this.title = todo.title;
  }
  cancel(): void { this.editingId = null; this.title = ''; }
  toggle(id: string): void { this.store.dispatch(new ToggleTodo(id)); }
  remove(id: string): void {
    this.store.dispatch(new DeleteTodo(id));
    if (this.editingId === id) this.cancel();
  }
}

4. Register the state at bootstrap

Replace src/main.ts with the bootstrap below. For an existing application, merge provideStore([TodosState]) into its application providers rather than discarding routing or other configuration.

src/main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { provideStore } from '@ngxs/store';
import { App } from './app/app';
import { TodosState } from './app/todos.state';

bootstrapApplication(App, {
  providers: [provideStore([TodosState])]
}).catch(console.error);
Terminal
npm start

5. Check the behavior

  • Add two different titles and confirm both remain in the list.
  • Mark one complete, rename it, and confirm it stays complete.
  • Try a whitespace-only title; it should not create an item.
  • Edit an item, delete it and confirm the editor clears.
  • Reload the page; the list resets because this example has no persistence.

Moving from a demonstration to a product

For a server-backed version, add a service for the real API. Return the HTTP Observable from asynchronous NGXS action handlers so dispatch completion reflects the request. Use the server’s identifiers and response data, and design loading, failure and concurrency behavior before clearing a form.

Documentation and references

AngularNGXSState management
Need help building this for your product?

Working with Angular and NGXS? Tell us what you're building and our engineers will tell you how they would approach it.

Talk to our team →

More from the blog