1.1.1 • Published 6 years ago

ngx-subscribe v1.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

npm version

ngx-subscribe

Subscribe decorator to be used in Angular 2+ components, it will automatically subscribe and cleanup observables to component properties.

Motivation

Allows to avoid boilerplate code when working with many observables. Decorator will subscribe to observable and unsubscribe from it when component will be destroyed. No need to use async pipe in the views. Supports inheritance.

Installation

npm install ngx-subscribe --save

Dependancies

ngx-subscribe requires rxjs of version 5.0.0 and above as peer dependacny and, to function 100% correctly, must be used within Angular components only.

Usage

demo.component.ts

import { Component, ChangeDetectorRef } from '@angular/core';
import { Http } from '@angular/http';
import { Subscribe, WithSubscriptions } from 'ngx-subscribe';

@Component({
    select: 'demo',
    templateUrl: 'demo.component.html'
})
export class DemoComponent implements WithSubscriptions {
    constructor(
        public changeDetector: ChangeDetectorRef,   // need that for OnPush change detection strategy to work correctly
        private _http: Http
    ) {
    }

    @Subscribe() pagesCount = this._http
        .get('http://example.com/pages/count')
        .map(response => response.json());

    // with default value
    @Subscribe(1) currentPage = this._http
        .get('http://example.com/pages/current')
        .map(response => response.json());
}

demo.component.html

Current page is {{ currentPage }} out of {{ pagesCount }} pages.
1.1.1

6 years ago

1.1.0

7 years ago

1.1.0-alpha.1

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago