What is ngx-cookie-service?
The ngx-cookie-service is an Angular service for handling cookies. It provides a simple API to set, get, and delete cookies in an Angular application.
What are ngx-cookie-service's main functionalities?
Set a Cookie
This feature allows you to set a cookie with a specified name and value. In this example, a cookie named 'test' with the value 'Hello World' is set.
import { CookieService } from 'ngx-cookie-service';
constructor(private cookieService: CookieService) { }
this.cookieService.set('test', 'Hello World');
Get a Cookie
This feature allows you to retrieve the value of a cookie by its name. In this example, the value of the cookie named 'test' is retrieved and logged to the console.
import { CookieService } from 'ngx-cookie-service';
constructor(private cookieService: CookieService) { }
const value = this.cookieService.get('test');
console.log(value);
Delete a Cookie
This feature allows you to delete a cookie by its name. In this example, the cookie named 'test' is deleted.
import { CookieService } from 'ngx-cookie-service';
constructor(private cookieService: CookieService) { }
this.cookieService.delete('test');
Check if a Cookie Exists
This feature allows you to check if a cookie exists by its name. In this example, it checks if the cookie named 'test' exists and logs the result to the console.
import { CookieService } from 'ngx-cookie-service';
constructor(private cookieService: CookieService) { }
const exists = this.cookieService.check('test');
console.log(exists);
Other packages similar to ngx-cookie-service
ngx-cookie
ngx-cookie is another Angular package for handling cookies. It provides similar functionalities to ngx-cookie-service, such as setting, getting, and deleting cookies. However, ngx-cookie also offers additional features like cookie serialization and deserialization.
angular2-cookie
angular2-cookie is an older package for managing cookies in Angular applications. It offers basic cookie operations similar to ngx-cookie-service but is less actively maintained and may not support the latest Angular versions.
NGX Cookie Service
An (AOT ready) Angular (4+) service for cookies. Originally based on the ng2-cookies library.
Installation
npm install ngx-cookie-service --save
yarn add ngx-cookie-service
Add the cookie service to your app.module.ts
as a provider:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { CookieService } from 'ngx-cookie-service';
@NgModule({
declarations: [ AppComponent ],
imports: [ BrowserModule, FormsModule, HttpModule ],
providers: [ CookieService ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Then, import and inject it into a component:
import { Component, OnInit } from '@angular/core';
import { CookieService } from 'ngx-cookie-service';
@Component({
selector: 'demo-root',
templateUrl: './app.component.html',
styleUrls: [ './app.component.scss' ]
})
export class AppComponent implements OnInit {
cookieValue = 'UNKNOWN';
constructor( private cookieService: CookieService ) { }
ngOnInit(): void {
this.cookieService.set( 'Test', 'Hello World' );
this.cookieValue = this.cookieService.get('Test');
}
}
That's it!
Methods
check( name: string ): boolean;
const cookieExists: boolean = cookieService.check('test');
get( name: string ): string;
const value: string = cookieService.get('test');
getAll(): {};
const allCookies: {} = cookieService.getAll();
set( name: string, value: string, expires?: number | Date, path?: string, domain?: string, secure?: boolean ): void;
cookieService.set( 'test', 'Hello World' );
delete( name: string, path?: string, domain?: string ): void;
cookieService.delete('test');
deleteAll( path?: string, domain?: string ): void;
cookieService.deleteAll();
Author
This cookie service is brought to you by 7leads GmbH. We built it for one of our apps, because the other cookie packages we found were either not designed "the Angular way" or caused trouble during AOT compilation.
Check out the GitHub page for more.
License
MIT