Skip to content

Instantly share code, notes, and snippets.

@svdoever
Created September 13, 2014 20:51
Show Gist options
  • Save svdoever/b9a27b1a824cdde323f6 to your computer and use it in GitHub Desktop.
Save svdoever/b9a27b1a824cdde323f6 to your computer and use it in GitHub Desktop.
LocalStorage service for AngularJS using TypeScript
/// <reference path='../_reference.ts'/>
module ft {
"use strict";
export class LocalStorageService {
// TODO: Need to handle QUOTA_EXCEEDED_ERR
public static $inject = [
"$log"
];
constructor(private $log: ng.ILogService) {
}
read(path: string): any {
// if not in local storage, the string "undefined" is returned (why???)
var text: string = localStorage.getItem(path);
if (text === null || typeof text === "undefined" || text === "undefined") {
this.$log.debug("LocalStorageService::read(" + path + ") - path not found, returned null");
return null;
}
else {
this.$log.debug("LocalStorageService::read(" + path + ")");
return text;
}
}
readObject<T>(path): T {
var text: any = this.read(path);
var data: T;
try {
data = <T>JSON.parse(text);
} catch (error) {
this.$log.error("LocalStorageService::readObject: can't convert string from local storage to object using JSON.parse(). Error: " + error);
data = null;
}
return data;
}
write(path: string, text: string): void {
this.$log.debug("LocalStorageService::write(" + path + ")");
localStorage.setItem(path, text);
}
writeObject(path: string, data: any): void {
var text: string = JSON.stringify(data);
this.write(path, text);
}
remove(path: string): void {
this.$log.debug("LocalStorageService::remove(" + path + ")");
localStorage.removeItem(path);
}
clear(): void {
this.$log.debug("LocalStorageService::clear - all items removed from local storage");
localStorage.clear();
}
}
}
@TheMagnificent11
Copy link

I tried this out but the casting to the generic type doesn't seem to work.

It just returns a JSON string. No exception occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment