Skip to content

Installation

ngx-cookie-service is an Angular service to read, set and delete browser cookies. Originally based on the ng2-cookies library, it is about 1.3 kB minified and gzipped.

Terminal window
npm install ngx-cookie-service

CookieService is provided in the root injector, so there’s nothing to register. Inject it wherever you need it:

theme-toggle.component.ts
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { CookieService } from 'ngx-cookie-service';
@Component({
selector: 'app-theme-toggle',
template: `<button type="button" (click)="toggle()">Theme: {{ theme() }}</button>`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ThemeToggleComponent {
private readonly cookieService = inject(CookieService);
protected readonly theme = signal(this.cookieService.get('theme') || 'light');
protected toggle(): void {
const next = this.theme() === 'light' ? 'dark' : 'light';
this.cookieService.set('theme', next, { expires: 365, path: '/' });
this.theme.set(next);
}
}

Each major version of ngx-cookie-service matches a major version of Angular. The current release supports Angular 22. See Versions for older releases.