Skip to content

Instantly share code, notes, and snippets.

@likwrk
Created April 11, 2017 14:46
Show Gist options
  • Save likwrk/bb40896de979ea4af156e83d169580a0 to your computer and use it in GitHub Desktop.
Save likwrk/bb40896de979ea4af156e83d169580a0 to your computer and use it in GitHub Desktop.
Let Operator in Angular
//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