Reading and writing
Change the name or value, then call each method. The table underneath is the result of getAll(), so it lists every cookie this page can read. Click the </> button to see the component’s source.
import { ChangeDetectionStrategy, Component, inject, signal, WritableSignal } from '@angular/core';import { CookieService } from 'ngx-cookie-service';
@Component({ selector: 'read-write-example', templateUrl: './read-write-example.component.html', changeDetection: ChangeDetectionStrategy.OnPush,})export class ReadWriteExampleComponent { private readonly cookieService: CookieService = inject(CookieService);
protected readonly name: WritableSignal<string> = signal('docs-theme'); protected readonly value: WritableSignal<string> = signal('dark'); /** The call that ran last and what it returned. */ protected readonly result: WritableSignal<string> = signal(''); /** Every cookie this page can read, as returned by getAll(). */ protected readonly cookies: WritableSignal<Record<string, string>> = signal(this.cookieService.getAll());
protected set(): void { this.cookieService.set(this.name(), this.value(), { path: '/' }); this.show(`set('${this.name()}', '${this.value()}', { path: '/' })`, undefined); }
protected get(): void { this.show(`get('${this.name()}')`, JSON.stringify(this.cookieService.get(this.name()))); }
protected check(): void { this.show(`check('${this.name()}')`, String(this.cookieService.check(this.name()))); }
protected delete(): void { this.cookieService.delete(this.name(), { path: '/' }); this.show(`delete('${this.name()}', { path: '/' })`, undefined); }
protected entries(): [string, string][] { return Object.entries(this.cookies()); }
private show(call: string, returned: string | undefined): void { this.result.set(returned === undefined ? `cookieService.${call}` : `cookieService.${call} → ${returned}`); this.cookies.set(this.cookieService.getAll()); }}<div class="docs-row"> <div class="docs-field"> <label for="read-write-name">Name</label> <input id="read-write-name" type="text" autocomplete="off" [value]="name()" (input)="name.set($any($event.target).value)" /> </div> <div class="docs-field"> <label for="read-write-value">Value</label> <input id="read-write-value" type="text" autocomplete="off" [value]="value()" (input)="value.set($any($event.target).value)" /> </div></div><div class="docs-actions"> <button type="button" class="primary" (click)="set()">set()</button> <button type="button" (click)="get()">get()</button> <button type="button" (click)="check()">check()</button> <button type="button" (click)="delete()">delete()</button></div>
@if (result()) { <pre class="docs-output" aria-live="polite">{{ result() }}</pre>}
<h4 class="docs-subheading">getAll()</h4>@if (entries().length) { <table class="docs-table"> <thead> <tr> <th scope="col">Name</th> <th scope="col">Value</th> </tr> </thead> <tbody> @for (entry of entries(); track entry[0]) { <tr> <td> <code>{{ entry[0] }}</code> </td> <td> <code>{{ entry[1] }}</code> </td> </tr> } </tbody> </table>} @else { <p class="docs-muted">No cookies are visible from this page yet. Click <strong>set()</strong>.</p>}get()returns''for a missing cookie. Usecheck()when an empty value and a missing cookie mean different things.getAll()decodes names and values. When several cookies share a name (for example with different paths), it keeps the one the browser lists first, the same oneget()returns.- Cookies are shared by every tab and every page on the same site and path, so cookies set by other examples on this site show up here too.