npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stanvanheumen/ngx-storage

v1.0.5

Published

A simple library that allows you use local storage in Angular 5+.

Downloads

142

Readme

ngx-storage

A simple library that allows you to use local storage in Angular 5+.

  • Use command + F or ctrl + F to search for a keyword.
  • Contributions welcome, please see contribution guide.

Features

  • :frog: Observable based
  • :camel: Easy implementation
  • :mouse: Lazy loading compatible
  • :sheep: Angular Universal compatible
  • :panda_face: Automatic JSON (de)serialization
  • :bird: Ahead-Of-Time compilation compatible
  • :hamster: Library can be consumed by Angular CLI, Webpack, or SystemJS

Demo

Click here to play with the example

Installation

To use ngx-storage in your project install it via npm or yarn:

$ npm install @stanvanheumen/ngx-storage --save

# or

$ yarn add @stanvanheumen/ngx-storage

Setup

Add the NgxStorageModule to your imports array in your CoreModule.

import {NgxStorageModule} from '@stanvanheumen/ngx-storage';

@NgModule({
    imports: [NgxStorageModule]
})
export class AppModule {}

Since the StorageService contains state it is important to only import the NgxStorageModule in your CoreModule.

API

The StorageService has the following API:

.get<T>(token: string): Observable<T | null>

Returns the value associated to the specified token wrapped in an observable stream that will get updated each time the user sets a new value in the storage.

// For a primitive type like string, number and boolean.
const string$ = this.storage.get<string>('my-string');

// For an advanced object or array.
const object$ = this.storage.get<MyAdvancedObject>('my-advanced-object');

The value is deserialized using the JSON.parse() method.

.set<T>(token: string, data: T): void

Associates a value to the specified token.

// For a primitive type like string, number and boolean.
this.storage.set<string>('my-local-storage-token', 'my-new-value');

// For an advanced object or array.
this.storage.set<MyAdvancedObject>('my-advanced-object', {name: 'Test', description: 'Lorem Ipsum'});

The value is serialized using the JSON.stringify() method.

.remove(token: string): void

Removes the value associated to the specified token.

this.storage.remove('my-local-storage-token');

.clear(): void

Removes all key-value pairs from the storage.

this.storage.clear();

Example

import {Component, OnInit} from '@angular/core';
import {StorageService} from '@stanvanheumen/ngx-storage';
import {Observable} from 'rxjs/Observable';

@Component({
    selector: 'app-root',
    template: `
        <pre><code>Current value in the local storage : {{ data$ | async | json }}</code></pre>
        
        <button (click)="setStorageValue('Awesome')">Set value to <strong>"Awesome"</strong></button>
        <button (click)="setStorageValue('Cool')">Set value to <strong>"Cool"</strong></button>
        <button (click)="setStorageValue('Hello world!')">Set value to <strong>"Hello world!"</strong></button>
        <button (click)="removeStorageValue()">Clear the value</button>
    `
})
export class AppComponent implements OnInit {
    
    data$: Observable<string>;
    
    constructor(private storage: StorageService) {
    }
    
    ngOnInit() {
        this.data$ = this.storage.get<string>('my-local-storage-token');
    }
    
    setStorageValue(value: string) {
        this.storage.set<string>('my-local-storage-token', value);
    }
    
    removeStorageValue() {
        this.storage.remove('my-local-storage-token');
    }

}