forked from rrgarciach/angular2-local-storage
-
Notifications
You must be signed in to change notification settings - Fork 2
/
local_storage.ts
36 lines (29 loc) · 892 Bytes
/
local_storage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
export class LocalStorage {
public localStorage:any;
constructor() {
try{
if (!localStorage) {
throw new Error('Current browser does not support Local Storage');
}
this.localStorage = localStorage;
} catch(err){}
}
public set(key:string, value:string):void {
this.localStorage[key] = value;
}
public get(key:string):string {
return this.localStorage[key] || false;
}
public setObject(key:string, value:any):void {
this.localStorage[key] = JSON.stringify(value);
}
public getObject(key:string):any {
return JSON.parse(this.localStorage[key] || '{}');
}
public remove(key:string):any {
this.localStorage.removeItem(key);
}
}
export const LOCAL_STORAGE_PROVIDERS:any[] = [
{provide: LocalStorage, useClass: LocalStorage},
];