angular2-storage-sync
0.1.0 • Public • Published Angular2 Storage Sync
Angular2 decorator to sync properties automatically from/to LocalStorage or SessionStorage
Use
npm install --save angular2-storage-sync
- Use in your component:
import { StorageSync, StorageStrategy } from 'angular2-storage-sync';
export class AppComponent {
@StorageSync('rememberMe') remember: boolean = false;
@StorageSync(null, StorageStrategy.Session) items: Array<Object> = [];
}
The @StorageSync expects two params:
- Custom key - the default is the property name.
- StorageStrategy - Local or Session - default to LocalStorage.
Important:
Define always a default value at the property you are using @StorageSync
.
Example
@Component({
selector: 'storage-app',
template:
<input type="checkbox" [(ngModel)]="remember"> Remember me
<button (click)="setItems()">Set items</button>
<ul>
<li *ngFor="let item of items">
{{item.id}}
</li>
</ul>
})
export class AppComponent {
@StorageSync('rememberMe') remember: boolean = false;
@StorageSync(null, StorageStrategy.Session) items: Array<Object> = [];
setItems() {
this.items = [{id: 1}, {id: 2}];
}
}
``
Package Sidebar
Install
npm i angular2-storage-sync
Weekly Downloads