Created
April 5, 2016 15:00
-
-
Save kmassada/008be87363d31efe25f9add426b1e322 to your computer and use it in GitHub Desktop.
TypeScript Angular Factory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface IStorageService { | |
set(key: string, value: any): void; | |
remove(key: string): void; | |
get(key: string, defaultValue: any): any; | |
setObject(key: string, value: any): void; | |
getObject(key: string): any; | |
removeObject (key: string): void; | |
} | |
class StorageService implements IStorageService { | |
private storage: any; | |
constructor(private $window: angular.IWindowService) { | |
this.storage = this.storage; | |
} | |
set(key: string, value: any) { | |
this.storage[key] = value; | |
}; | |
get(key: string, defaultValue: any) { | |
return this.storage[key] || defaultValue; | |
}; | |
remove(key: string) { | |
this.storage.removeItem(key); | |
}; | |
setObject(key: string, value: any) { | |
this.storage[key] = JSON.stringify(value); | |
}; | |
getObject(key: string) { | |
return JSON.parse(this.storage[key] || "{}"); | |
}; | |
removeObject(key: string) { | |
this.remove(key); | |
}; | |
} | |
factory.$inject = ["$window"]; | |
function factory($window: angular.IWindowService): IStorageService { | |
return new StorageService($window); | |
} | |
angular | |
.module("Services.StorageService", []) | |
.factory("storageService", factory); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment