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

ngx-value

v1.0.5

Published

💫 Load and access JSON data with this easy-to-use @Value decorator for Angular 💫

Downloads

15

Readme

ngx-value

npm npm GitHub last commit Build Status GitHub

ngx-value is an Angular library that provides an easy way to load JSON data from various HTTP sources and read its properties during runtime.

A working demo can be found here

Goal

There's always values we need to tweak and adjust depending on the environment, even though the project being deployed is still the same one. Or maybe we're just testing things out in an environment of our own and we'd like to make use of different configuration files and keep organized.

Anyhow, we can now use ngx-value to load a set of JSON data from any kind of source over HTTP and initialize variables with their properties by using the @Value decorator or just Get() them later on.

Installation

To use ngx-value in your project just run:

npm install ngx-value

API:

Decorators

@Value(name: string, path?: string): any
@Path(path: string): any

Methods

Values(...paths: string[]): Promise<any>
Get(property: string, path?: string): any

Usage

First off, it is necessary to load any data before our angular application starts and as such we need to include some initialization logic into our app. As seen below, we make use of the APP_INITIALIZER provided by Angular which will allow us to run some code before the app actually starts running.

Afterwards just use @Value or Get() to retrieve any property.

By default, @Value will retrieve a property based on its path parameter. Otherwise, it will go for the class path set with @Path. If no path was set that way, it will use the first path passed in @Values(...), and as last resort it will use assets/properties.json as the default path.

Keep in mind that you are allowed to call Values(...) at any time during runtime to continue loading JSON data into memory, wait for it to finish loading before attempting to use its properties though (it's a Promise).

Example

/**
 *  `app.module.ts`
 */

import { Values } from 'ngx-value';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [
    {
      provide: APP_INITIALIZER,
      useFactory: () => () => Values('assets/properties.json', 'assets/websites.json', 'assets/authors.json'),
      multi: true
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
/**
 *  `app.component.ts`
 */

import { Value, Get } from 'ngx-value';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
@Path('assets/properties.json') // Default path for this class
export class AppComponent implements OnInit {

  @Value('title') // default location is "assets/properties.json"
  public title: string = '- Not Found -'; // Default value is "- Not Found -"

  @Value('angular', 'assets/websites.json') // load from "assets/websites.json"
  public angular: string = '- Not Found -';

  @Value(null, 'assets/authors.json') // null means it's an array
  public authors: any = [];
  
  @Value('some-property', 'http://127.0.0.1/anywhere.json') // load data from some other site
  public anywhere: string = '- Not Found -';

  ngOnInit(): void {
    this.random = Get('random');
  }
}