Skip to content

Instantly share code, notes, and snippets.

@av1v3k
Created March 7, 2023 06:45
Show Gist options
  • Save av1v3k/ed9dfaaff5e0f2b9f09e387a464c5ff2 to your computer and use it in GitHub Desktop.
Save av1v3k/ed9dfaaff5e0f2b9f09e387a464c5ff2 to your computer and use it in GitHub Desktop.
Validating with Service call on typing text - Angular
searchSubject = new Subject();
searchResult$: Observable<any>;
this.searchResult$ = this.searchSubject.pipe(
debounceTime(1000),
distinctUntilChanged(),
switchMap((searchText: string) => {
return this.service.validateZipCode(searchText)
}),
map((res: any) => {
return { ...res }
})
);
this.searchResult$.subscribe(
data => {
console.warn('subscribe', data);
if (data.hasOwnProperty('errorMessage')) {
this.setAddressFormErrored(data.errorMessage);
} else {
this.setAddressFormErrored('');
this.userData.city = data.city;
this.userData.state = data.state;
this.validateStateAfterResponse(data.state);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment