Created
October 9, 2020 15:38
-
-
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
This file contains 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
@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