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.
Install
Section titled “Install”npm install ngx-cookie-servicepnpm add ngx-cookie-serviceyarn add ngx-cookie-serviceInject the service
Section titled “Inject the service”CookieService is provided in the root injector, so there’s nothing to register. Inject it wherever you need it:
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); }}Compatibility
Section titled “Compatibility”Each major version of ngx-cookie-service matches a major version of Angular. The current release supports Angular 22. See Versions for older releases.
Next steps
Section titled “Next steps”UsageRead, write, check and delete cookies, and choose the right options.
Live examplesEvery feature running on the page, with its source code.