Skip to content

Instantly share code, notes, and snippets.

@cartant
Last active June 27, 2018 01:07
Show Gist options
  • Select an option

  • Save cartant/75582f3791714669eefa736c07b5fd95 to your computer and use it in GitHub Desktop.

Select an option

Save cartant/75582f3791714669eefa736c07b5fd95 to your computer and use it in GitHub Desktop.
import { Component } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Observable } from 'rxjs';
import { debounceTime, distinctUntilChanged, pluck } from 'rxjs/operators';
@Component({
selector: 'some-component',
template: `
<form [formGroup]="form">
<input formControlName="term" type="text">
</form>
<div class="searching" *ngIf="term$ | async as term">
<span>Searching for {{ term }}</span>
</div>
`
})
export class SomeComponent {
form: FormGroup;
term$: Observable<string>;
constructor(formBuilder: FormBuilder) {
this.form = formBuilder.group({
term: ['']
});
this.term$ = this.form.valueChanges.pipe(
pluck('term'),
debounceTime(400),
distinctUntilChanged()
) as Observable<string>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment