Skip to content

An (AOT ready) Angular (4+) service for cookies. Originally based on the `ng2-cookies` library.

License

Notifications You must be signed in to change notification settings

orlandoangular/ngx-cookie-service

 
 

Repository files navigation

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

# or

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!

What to do now?

  • Run npm run test to run the tests for the cookie service (located in the demo-app folder)
  • Have a look at and play around with the demo-app to get to know the cookie service with npm run start (open http://localhost:4200/ in your favourite browser)
  • If you do not want to install this via NPM, you can run npm run compile and use the *.d.ts and *.js files in the dist-lib folder

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.

Contributors

Pull requests are welcome! Thanks to all contributors:

License

MIT

About

An (AOT ready) Angular (4+) service for cookies. Originally based on the `ng2-cookies` library.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 72.6%
  • JavaScript 25.1%
  • HTML 1.9%
  • CSS 0.4%