Created
April 11, 2017 14:46
-
-
Save likwrk/bb40896de979ea4af156e83d169580a0 to your computer and use it in GitHub Desktop.
Let Operator in Angular
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//our root app component | |
import {Component, NgModule, Output, EventEmitter, Input} from '@angular/core' | |
import {BrowserModule} from '@angular/platform-browser' | |
import {ReactiveFormsModule, FormControl} from '@angular/forms'; | |
import {Observable} from 'rxjs/Rx'; | |
@Component({ | |
selector: 'app-input', | |
template: ` | |
<input type="text" [formControl]="form" class="form-control"> | |
`, | |
}) | |
export class AppInput { | |
@Output() value = new EventEmitter(); | |
@Input() middleware: Observable<any> = obs => obs; | |
form; | |
ngOnInit() { | |
this.form = new FormControl(); | |
this.form.valueChanges.let(this.middleware).subscribe(val => { | |
this.value.next(val); | |
}); | |
} | |
ngOnDestory() { | |
// destroy | |
} | |
} | |
@Component({ | |
selector: 'my-app', | |
template: ` | |
<app-input (value)="doSomething($event)" [middleware]="middleware"></app-input> | |
`, | |
}) | |
export class App { | |
middleware = obs => obs.debounceTime(1000).filter(val => val !== "test"); | |
constructor() { | |
console.clear(); | |
} | |
doSomething(val) { | |
console.log(val); | |
} | |
} | |
@NgModule({ | |
imports: [ BrowserModule, ReactiveFormsModule ], | |
declarations: [ App, AppInput ], | |
bootstrap: [ App ] | |
}) | |
export class AppModule {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment