Skip to content

Instantly share code, notes, and snippets.

@jagomf
Created December 20, 2023 08:03
Show Gist options
  • Save jagomf/8fba712eb06bb87018dc634fcaca97a8 to your computer and use it in GitHub Desktop.
Save jagomf/8fba712eb06bb87018dc634fcaca97a8 to your computer and use it in GitHub Desktop.
Combine values collected from URL bound to inputs
@Component({
template: `
<div *ngIf="data$ | async as data">
{{ data }}
</div>
`
})
export class SearchComponent implements OnInit {
private dataService = inject(DataService);
id$ = new BehaviorSubject<string | null>(null);
query$ = new BehaviorSubject<string | null>(null);
@Input() set id(id: string) { this.id$.next(id); }
@Input() set query(query: string) { this.query$.next(query); }
data$ = combineLatest([
this.id$.pipe(filter(id => id !== null)),
this.query$.pipe(filter(query => query !== null))
]).pipe(
switchMap(([id, query]) => this.dataService.getData(id, query))
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment