• Stars
    star
    302
  • Rank 138,030 (Top 3 %)
  • Language
    TypeScript
  • Created almost 9 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Angular 2+ decorator to save and restore variables/class properties to HTML5 LocalStorage automatically.

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.

Seeking new maintainer

This project is not maintained. Please consider taking it over. More information at https://github.com/open-source-chest/take-it-over. If you are looking for an alternativ right now, look at https://github.com/zoomsphere/ngx-store/.

Use

  1. Download the library using npm or github: npm install --save angular2-localstorage

  2. Import the WebStorageModule in your app module:

    import {Component} from "angular2/core";
    import {WebStorageModule, LocalStorageService} from "angular2-localstorage";
    
    @NgModule({
        import: [WebStorageModule]
    @Component({
        providers: [LocalStorageService]
    })
    export class AppModule {}
  3. Use the LocalStorage decorator

import {LocalStorage, SessionStorage} from "angular2-localstorage/WebStorage";

class MySuperComponent {
    @LocalStorage() public lastSearchQuery:Object = {};
    @LocalStorage('differentLocalStorageKey') public lastSearchQuery:Object = {};
}

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

@Component({
    selector: 'app-login',
    template: `
<form>
    <div>
        <input type="text" [(ngModel)]="username" placeholder="Username" />
        <input type="password" [(ngModel)]="password" placeholder="Password" />
    </div>
    
    <input type="checkbox" [(ngModel)]="rememberMe" /> Keep me logged in

    <button type="submit">Login</button>
</form>
    `
})
class AppLoginComponent {
    //here happens the magic. `username` is always restored from the localstorage when you reload the site
    @LocalStorage() public username:string = '';
    
    public password:string;
    
    //here happens the magic. `rememberMe` is always restored from the localstorage when you reload the site
    @LocalStorage() public rememberMe:boolean = false;
}
@Component({
    selector: 'admin-menu',
    template: `
<div *ngFor="#menuItem of menuItems() | mapToIterable; #i = index">
    <h2 (click)="hiddenMenuItems[i] = !!!hiddenMenuItems[i]">
        {{i}}: {{category.label}}
    </h2>
    <div style="padding-left: 15px;" [hidden]="hiddenMenuItems[i]">
        <a href>Some sub menu item 1</a>
        <a href>Some sub menu item 2</a>
        <a href>Some sub menu item 3</a>
    </div>
</div>
    `
})
class AdminMenuComponent {
    public menuItems = [{title: 'Menu1'}, {title: 'Menu2'}, {title: 'Menu3'}];

    //here happens the magic. `hiddenMenuItems` is always restored from the localstorage when you reload the site
    @LocalStorage() public hiddenMenuItems:Array<boolean> = [];
    
    //here happens the magic. `profile` is always restored from the sessionStorage when you reload the site from the current tab/browser. This is perfect for more sensitive information that shouldn't stay once the user closes the browser.
    @SessionStorage() public profile:any = {};
}

More Repositories

1

css-element-queries

CSS Element-Queries aka Container Queries. High-speed element dimension/media queries in valid css.
JavaScript
4,273
star
2

TypeRunner

High-performance TypeScript compiler
C++
2,595
star
3

jquery-selectBox

A jQuery plugin for replacing <select> elements.
JavaScript
548
star
4

topsort.php

High-Performance Topological Sort / Dependency resolver in PHP
PHP
239
star
5

php-rest-service

Php-Rest-Service is a very simple and fast PHP class for server-side RESTful JSON APIs.
PHP
216
star
6

BetterQuitJobBundle

You should better quit your current job instead of searching a solution for *that* problem.
95
star
7

Pesto

Pesto is a high-performance GUI framework in C++ highly inspired by CSS and HTML, using Skia as rendering engine.
C++
67
star
8

angular-es6-annotations

This little collection of annotations and registry functions allows you to register directives, controllers and filter with annotations at your angular module.
JavaScript
66
star
9

angular-desktop-ui

Angular & Electron desktop UI framework. Angular components for native looking and behaving macOS desktop UI (Electron/Web)
TypeScript
55
star
10

pybridge

TypeScript library to access python functions in NodeJS, type-safe and easy to use.
TypeScript
49
star
11

typescript-react-dependency-injection

React + Vite + Deepkit's powerful dependency injection system
TypeScript
30
star
12

typedoc-plugin-lerna-packages

A plugin for Typedoc that groups all Lerna packages into own TS module
TypeScript
28
star
13

npm-local-development

Replacement for 'npm link' that actually works. Automatically overwrites installed packages with a locally available dev version.
JavaScript
25
star
14

glut.ts

The first reactive full-stack framework for Typescript specially tailored for admin interfaces and complex SPA.
TypeScript
24
star
15

bitcoin.ts

Typescript implementation of a Cryptocurrency inspired by the Bitcoin blockchain.
TypeScript
22
star
16

chrome-chatgpt-screenshot-extension

JavaScript
12
star
17

google-api-php-client

Google APIs Client Library for PHP
PHP
11
star
18

krycms-bundle-old

Don't use it. Don't Star it. Old project. New Project is https://github.com/jarves/jarves |
JavaScript
10
star
19

angular-typescript-decorators

Kinda hackish handy decorators for using Typescript with Angular v1.4+
TypeScript
9
star
20

katana-orm

Katana ORM, the new incredible sharp PHP ORM - maybe someday.
8
star
21

typescript-semantic-search

TypeScript
5
star
22

angular-zoneless

Angular v16+ Zoneless implementation for SSR with Hydration
TypeScript
5
star
23

optimistic-locking-behavior

A behavior allowing you to use optimistic locking in Propel 2
PHP
5
star
24

twig-apply_filter-bundle

Symfony Bundle for Twig 'apply_filter' to call dynamic filters.
PHP
4
star
25

deepkit-bookstore

Example bookstore API with Deepkit API Console
TypeScript
3
star
26

angular-unsub

Unsubscribes automatically your RXJS subscriptions when component is ngOnDestroy'ed
TypeScript
3
star
27

change-logger-behavior

Logs changes for defined columns in a extra logger table, for Propel2
PHP
3
star
28

glut-admin

The first real-time headless CMS/CMF with crud builder, based on Typescript/Angular. So efficient and fast it hurts.
TypeScript
2
star
29

mootools-sprintf

Mootools Javascript sprintf/printf function.
JavaScript
2
star
30

estdlib.ts

Engineer's strict stdlib for TypeScript.
TypeScript
2
star
31

mowla.js

A fast and super simple JavaScript Template Engine.
JavaScript
1
star
32

shellfuncs

Some useful bash functions
Shell
1
star
33

deepkit-webpack

JavaScript
1
star
34

git-objects-sync

git push based on objects, instead of commits.
Python
1
star
35

Propel2-old

PHP
1
star
36

angular-ivy-issue1

TypeScript
1
star
37

vscode-vertical-tabs

1
star
38

shut-the-lock

iOs Game "Shut the lock"
JavaScript
1
star
39

docker-macOS-container

An easy way to run commands in docker using macOS catalina as OS (via qemu) via ssh. Jenkins node possible
Shell
1
star
40

angular-desktop-ui-example

Angular v9 with angular-desktop-ui configured and ready to use
TypeScript
1
star
41

docker-pytorch

Pytorch v2+ in docker with ARM support
Dockerfile
1
star
42

angular-marshal-example

TypeScript
1
star
43

ts-bug

JavaScript
1
star