Angular2 @LocalStorage
This little Angular2/typescript decorator makes it super easy to save and restore automatically a variable state in your directive (class property) using HTML5' LocalStorage.
Use
-
Download the library using npm or github:
npm install --save angular2-localstorage
-
Register the LocalStorage in your
boot.ts
:;;// register LocalStorage, this registers our change-detection.LocalStorageSubscriberappPromise;or in your root component:
;; -
Use the
LocalStorage
decorator
;
Note: Define always a default value at the property you are using @LocalStorage
.
Note: The localstorage key is per default the property name. Define the first argument of @LocalStorage
to set different one.
Note: Please don't store circular structures as this library uses JSON.stringify to encode before using LocalStorage.
Examples