Giter Site home page Giter Site logo

ngx-local's Introduction

ngx-local

npm version

MIT License

Structural directive for Angular 2.x+ to locally store a value in a template input variable.

This is particularly useful when used in conjunction with the 'async' pipe:

<div *ngxLocal="httpUser$ | async; let user">
  <div>id: {{ user?.id }}</div>
  <div>name: {{ user?.name }}</div>
  <div>username: {{ user?.username }}</div>
  <div>email: {{ user?.email }}</div>
</div>

Installation

Via npm

npm install ngx-local --save

Via yarn

yarn add ngx-local

Then add the directive to your app.module.ts:

// ...
import { NgxLocal } from 'ngx-local';

@NgModule({
  // ...
  declarations: [
    // ...
    NgxLocal
  ],
  // ...
})
export class AppModule {
}

Sample usage

import { Component } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

@Component({
  selector: 'my-app',
  template: `
    <div *ngxLocal="httpUser$ | async; let user">
      <div>id: {{ user?.id }}</div>
      <div>name: {{ user?.name }}</div>
      <div>username: {{ user?.username }}</div>
      <div>email: {{ user?.email }}</div>
    </div>
  `,
})
export class AppComponent {
  httpUser$: Observable<User>;

  constructor(private http: Http) {
    const randomUserId = Math.floor(Math.random() * 10) + 1;

    this.httpUser$ = this.http
      .get(`https://jsonplaceholder.typicode.com/users/${randomUserId}`)
      .map((res: Response): User => res.json() || {});
  };
}

export interface User {
  id: number;
  name: string;
  username: string;
  email: string;
}

TO-DO

  • Release!
  • Add tests

ngx-local's People

Contributors

jsayol avatar

Stargazers

Jordi Collell avatar Alex Jover avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.