Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianvink/8791705ea0cd3e6b24e9bc2e79ea667a to your computer and use it in GitHub Desktop.
Save ianvink/8791705ea0cd3e6b24e9bc2e79ea667a to your computer and use it in GitHub Desktop.
Using SwitchMap to review an observable and then poll for another via a async call
@Component({
template: `
<input [formControl]="petControl"/>
<span *ngIf="isPetExists">Pet already exists!</span>
`
})
export class PetComponent implements OnInit {
petControl = new FormControl('');
isPetExists = false;
constructor(private service: Service) {}
ngOnInit() {
this.petControl.valueChanges
.pipe(debounceTime(500),
switchMap((value: string) => {
return this.service.isPetExists(value);
}).subscribe((isPetExists: boolean) => {
this.isPetExists = isPetExists;
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment