Default options
Instead of repeating path, secure and sameSite in every call, set them once in your application config:
import { ApplicationConfig } from '@angular/core';import { provideCookieOptions } from 'ngx-cookie-service'; // or 'ngx-cookie-service-ssr'
export const appConfig: ApplicationConfig = { providers: [provideCookieOptions({ path: '/', secure: true, sameSite: 'Strict' })],};The defaults apply to every set(), delete() and deleteAll() call.
How defaults combine with call options
Section titled “How defaults combine with call options”- Options passed to a call win over the defaults.
undefinedvalues in a call don’t override a default.- A call that passes its own
expires(ormaxAgewithngx-cookie-service-ssr) replaces the default lifetime. That’s whydelete()still expires the cookie even when the defaults set a longexpires.
// Defaults: { path: '/', expires: 30, sameSite: 'Strict' }cookieService.set('a', '1'); // path=/, expires in 30 days, SameSite=StrictcookieService.set('b', '2', { sameSite: 'Lax' }); // path=/, expires in 30 days, SameSite=LaxcookieService.set('c', '3', { path: undefined }); // path=/ (undefined doesn't override)cookieService.delete('a'); // path=/, expired nowSee it running in the Default options example.