Small and focused
One injectable service, about 1.3 kB minified and gzipped. No dependencies besides Angular.
This is the real library running on this page. Type a name, then reload the page.
import { ChangeDetectionStrategy, Component, inject, signal, WritableSignal } from '@angular/core';import { CookieService } from 'ngx-cookie-service';
@Component({ selector: 'quick-start-example', templateUrl: './quick-start-example.component.html', changeDetection: ChangeDetectionStrategy.OnPush,})export class QuickStartExampleComponent { private readonly cookieService: CookieService = inject(CookieService);
/** The saved name, read from the cookie when the component starts. Reload the page: it's still there. */ protected readonly name: WritableSignal<string> = signal(this.cookieService.get('docs-name'));
protected save(value: string): void { this.cookieService.set('docs-name', value.trim(), { expires: 7, path: '/' }); this.name.set(this.cookieService.get('docs-name')); }
protected forget(): void { this.cookieService.delete('docs-name', { path: '/' }); this.name.set(this.cookieService.get('docs-name')); }}@if (name()) { <p class="docs-greeting"> Welcome back, <strong>{{ name() }}</strong >! This was read with <code>cookieService.get('docs-name')</code>. Reload the page and it's still here. </p> <button type="button" (click)="forget()">Forget me</button>} @else { <form class="docs-row" (submit)="$event.preventDefault(); save(nameInput.value)"> <div class="docs-field"> <label for="quick-start-name">Your name</label> <input #nameInput id="quick-start-name" type="text" autocomplete="off" placeholder="Ada" required /> </div> <button type="submit" class="primary">Remember me for 7 days</button> </form>}npm install ngx-cookie-servicepnpm add ngx-cookie-serviceyarn add ngx-cookie-serviceimport { inject } from '@angular/core';import { CookieService } from 'ngx-cookie-service';
const cookieService = inject(CookieService);
cookieService.set('theme', 'dark', { expires: 30, path: '/' });cookieService.get('theme'); // 'dark'cookieService.delete('theme', { path: '/' });Small and focused
One injectable service, about 1.3 kB minified and gzipped. No dependencies besides Angular.
Server-side rendering
ngx-cookie-service-ssr reads the request’s cookies and writes Set-Cookie headers while Angular renders on the server.
Safe defaults
SameSite=Lax by default, Secure forced where browsers require it, and unsafe paths or domains rejected with a warning.
App-wide configuration
Set default options once with provideCookieOptions(), and choose how values are encoded with provideCookieEncoding().