ngx-subscribe
Version:
Subscribe decorator to be used in Angular 2+ components, it will automatically subscribe and cleanup observables to component properties.
66 lines (44 loc) • 1.78 kB
Markdown
[](https://badge.fury.io/js/ngx-subscribe)
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`
```typescript
import { Component, ChangeDetectorRef } from '@angular/core';
import { Http } from '@angular/http';
import { Subscribe, WithSubscriptions } from 'ngx-subscribe';
export class DemoComponent implements WithSubscriptions {
constructor(
public changeDetector: ChangeDetectorRef, // need that for OnPush change detection strategy to work correctly
private _http: Http
) {
}
pagesCount = this._http
.get('http://example.com/pages/count')
.map(response => response.json());
// with default value
currentPage = this._http
.get('http://example.com/pages/current')
.map(response => response.json());
}
```
### `demo.component.html`
```html
Current page is {{ currentPage }} out of {{ pagesCount }} pages.
```