Expiration
expires takes either a number of days or a Date. Without it, the cookie is a session cookie, deleted when the browser closes.
Set the 15-second cookie and watch check() turn false when it expires. The table below the demo reads the stored expiry date back from the browser.
import { ChangeDetectionStrategy, Component, DestroyRef, inject, signal, WritableSignal } from '@angular/core';import { CookieService } from 'ngx-cookie-service';import { CookieInspectorComponent } from '../shared/cookie-inspector.component';
interface ExpiryCase { name: string; call: string; set: () => void;}
@Component({ selector: 'expires-example', templateUrl: './expires-example.component.html', imports: [CookieInspectorComponent], changeDetection: ChangeDetectionStrategy.OnPush,})export class ExpiresExampleComponent { private readonly cookieService: CookieService = inject(CookieService);
protected readonly cases: ExpiryCase[] = [ { name: 'docs-session', call: `set('docs-session', 'yes', { path: '/' })`, set: () => this.cookieService.set('docs-session', 'yes', { path: '/' }), }, { name: 'docs-week', call: `set('docs-week', 'yes', { expires: 7, path: '/' })`, set: () => this.cookieService.set('docs-week', 'yes', { expires: 7, path: '/' }), }, { name: 'docs-15-seconds', call: `set('docs-15-seconds', 'yes', { expires: new Date(Date.now() + 15_000), path: '/' })`, set: () => this.cookieService.set('docs-15-seconds', 'yes', { expires: new Date(Date.now() + 15_000), path: '/' }), }, ];
protected readonly selected: WritableSignal<string> = signal('docs-week'); /** Bumped every second so check() runs again and the 15-second cookie can be seen expiring. */ protected readonly tick: WritableSignal<number> = signal(0);
constructor() { const timer = setInterval(() => this.tick.update((t) => t + 1), 1000); inject(DestroyRef).onDestroy(() => clearInterval(timer)); }
protected setCookie(expiryCase: ExpiryCase): void { expiryCase.set(); this.selected.set(expiryCase.name); this.tick.update((t) => t + 1); }
protected exists(name: string): boolean { this.tick(); return this.cookieService.check(name); }}<table class="docs-table"> <thead> <tr> <th scope="col">Call</th> <th scope="col">check()</th> <th scope="col"><span class="docs-visually-hidden">Actions</span></th> </tr> </thead> <tbody> @for (expiryCase of cases; track expiryCase.name) { <tr> <td> <code>{{ expiryCase.call }}</code> </td> <td> @if (exists(expiryCase.name)) { <span class="docs-pill docs-pill--yes">true</span> } @else { <span class="docs-pill">false</span> } </td> <td class="docs-cell-actions"> <button type="button" class="primary" (click)="setCookie(expiryCase)">Set</button> <button type="button" (click)="selected.set(expiryCase.name)" [attr.aria-pressed]="selected() === expiryCase.name">Inspect</button> </td> </tr> } </tbody></table>
<h4 class="docs-subheading"> Stored attributes of <code>{{ selected() }}</code></h4><docs-cookie-inspector [name]="selected()" [refresh]="tick()" />- Days can be fractional:
expires: 1 / 24is one hour. - An invalid
Date(such asnew Date('nope')) is ignored with a console warning, so the cookie becomes a session cookie. - To delete a cookie, use
delete()rather than setting a past date yourself. It also clears the value. - With
ngx-cookie-service-ssryou can also passmaxAgein seconds. When both are present, browsers useMax-Age.