12.0.6 • Published 1 year ago

observable-from-input v12.0.6

Weekly downloads
1
License
Apache License 2....
Repository
github
Last release
1 year ago

observable-from-input

Small library that provides a typesafe extension to use angular component input fields as RxJS Observables

Installation

npm i observable-from-input

Choose the version corresponding to your Angular version:

Angular@observable-from-input
12+12.x+
2 to 110.0.8 or less

Usage

Suppose you have the following component:

@Component({
    selector: 'app-component',
    ...
})
export class AppComponent {
    @Input() showData: boolean;
    data$: Observable<Data>;

    constructor(private store: Store) {
        this.data$ = this.store.pipe(
            select(selectData));
    }
}

In the constructor you want to connect the input to an observable. To do this you can now use the function fromInput. The signature of the function looks like this:

fromInput: <T>(target: T) => <K extends keyof T>(name: K) => Observable<T[K]>

The input can now be turned into an observable from the input field using the following call.

fromInput<AppComponent>(this)('showData');

Here the complete example:

import { fromInput } from 'observable-from-input';

@Component({
    selector: 'app-component',
    template: `
        <div *ngIf="showData">Input</div>
        <div *ngIf="showData$ | async">Input as observable</div>
    `
})
export class AppComponent {
    @Input() showData: boolean;
    showData$: Observable<boolean>;
    data$: Observable<Data>;

    constructor(private store: Store) {
        this.showData$ = fromInput<AppComponent>(this)('showData');
        this.data$ = this.store.pipe(
            select(selectData));

        // now you can connect observables as you like
    }
}
12.0.5

1 year ago

12.0.6

1 year ago

12.0.4

1 year ago

12.0.3

2 years ago

12.0.2

2 years ago

12.0.0

2 years ago

12.0.1

2 years ago

0.0.8

3 years ago

0.0.5

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.4

3 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago