angular2-cookie
![Downloads](http://img.shields.io/npm/dm/angular2-cookie.svg)
Implementation of Angular 1.x $cookies service to Angular 2 v1.2.3
Please use 1.1.x versions for angular2 beta, 1.2.2 version is for release candidates earlier than rc.5 and 1.2.3 is for >rc.5.
Table of contents:
Get Started
Installation
You can install this package locally with npm.
npm install angular2-cookie --save
After installing the library, it should be included in the SystemJS configurations.
(function (global) {
System.config({
paths: {
'npm:': 'node_modules/'
},
map: {
app: 'app',
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'rxjs': 'npm:rxjs',
'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
'angular2-cookie': 'npm:angular2-cookie'
},
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
'angular2-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
},
'angular2-cookie': {
main: './core.js',
defaultExtension: 'js'
}
}
});
})(this);
Usage
CookieService class is an injectable service with angular @Injectable()
decorator. Therefore, it needs to be registered in the providers array (encouraged way).
Then, it will be available in the constructor of the component class.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CookieService } from 'angular2-cookie/services/cookies.service';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
providers: [ CookieService ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
import {Component} from 'angular2/core';
import {CookieService} from 'angular2-cookie/core';
@Component({
selector: 'my-very-cool-app',
template: '<h1>My Angular2 App with Cookies</h1>'
})
export class AppComponent {
constructor(private _cookieService:CookieService){}
getCookie(key: string){
return this._cookieService.get(key);
}
}
Examples
Here you can find some usage examples with popular boilerplate libraries.
Angular2-quickstart
A boilerplate provided by Angular team.
(Link: https://github.com/angular/quickstart)
Just edit the systemjs.config.js
file and add the angular2-cookie
there.
(function (global) {
System.config({
paths: {
'npm:': 'node_modules/'
},
map: {
app: 'app',
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'rxjs': 'npm:rxjs',
'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
'angular2-cookie': 'npm:angular2-cookie'
},
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
'angular2-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
},
'angular2-cookie': {
main: './core.js',
defaultExtension: 'js'
}
}
});
})(this);
Angular2-seed
A popular seed project.
(Link: https://github.com/mgechev/angular2-seed)
Add the following settings to the (constructor of) ProjectConfig
class (path: ./tools/config/project.config.ts
).
this.mergeObject(this.SYSTEM_CONFIG_DEV['paths'], {'angular2-cookie': 'node_modules/angular2-cookie/bundles/angular2-cookie.js'});
this.mergeObject(this.SYSTEM_BUILDER_CONFIG['packages'], {
'angular2-cookie': {
main: 'core.js',
defaultExtension: 'js'
}
});
Do not forget to inject the service in the module (providers
array).
Angular-cli
A CLI tool for Angular2.
(Link: https://github.com/angular/angular-cli)
Edit the vendorNpmFiles
array (path: ./angular-cli-build.js
).
var Angular2App = require('angular-cli/lib/broccoli/angular2-app');
module.exports = function(defaults) {
return new Angular2App(defaults, {
vendorNpmFiles: [
'systemjs/dist/system-polyfills.js',
'systemjs/dist/system.src.js',
'zone.js/dist/**/*.+(js|js.map)',
'es6-shim/es6-shim.js',
'reflect-metadata/**/*.+(ts|js|js.map)',
'rxjs/**/*.+(js|js.map)',
'@angular/**/*.+(js|js.map)',
'angular2-cookie/**/*.+(js|js.map)'
]
});
};
Also add angular2-cookie
to the map
and packages
object in the system-config.ts
(path: ./src/system-config.ts
)
const map: any = {
'angular2-cookie': 'vendor/angular2-cookie'
};
const packages: any = {
'angular2-cookie': {main: 'core.js', defaultExtension: 'js'},
};
CookieService
There are 7 methods available in the CookieService
(6 standard methods from Angular 1 and 1 extra removeAll()
method for convenience)
get()
Returns the value of given cookie key.
get(key: string): string;
getObject()
Returns the deserialized value of given cookie key.
getObject(key: string): Object;
getAll()
Returns a key value object with all the cookies.
getAll(): any;
put()
Sets a value for given cookie key.
put(key: string, value: string, options?: CookieOptionsArgs): void;
putObject()
Serializes and sets a value for given cookie key.
putObject(key: string, value: Object, options?: CookieOptionsArgs): void;
remove()
Remove given cookie.
remove(key: string, options?: CookieOptionsArgs): void;
removeAll()
Remove all cookies.
removeAll(): void;
Options
Options object should be a type of CookieOptionsArgs
interface. The object may have following properties:
- path - {string} - The cookie will be available only for this path and its sub-paths. By default, this is the URL that appears in your
<base>
tag. - domain - {string} - The cookie will be available only for this domain and its sub-domains. For security reasons the user agent will not accept the cookie if the current domain is not a sub-domain of this domain or equal to it.
- expires - {string|Date} - String of the form "Wdy, DD Mon YYYY HH:MM:SS GMT" or a Date object indicating the exact date/time this cookie will expire.
- secure - {boolean} - If
true
, then the cookie will only be available through a secured connection.
Overriding default options globally
CookieService
can use ANGULAR2_COOKIES_PROVIDERS
to provide default options. Thus the default options can be altered by overriding the necessary class.
See Angular dependency injection guide for more information on the topic.
import {provide} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {App} from './myapp';
class MyOptions extends BaseCookieOptions {
path: string = '/my/path/';
}
bootstrap(App, [ANGULAR2_COOKIES_PROVIDERS, provide(CookieOptions, {useClass: MyOptions})]);
Note
The build process and the file structure of this repository has respectively modeled after the awesome angular2-google-maps project of Sebastian Müller.