Created
December 20, 2023 08:03
-
-
Save jagomf/8fba712eb06bb87018dc634fcaca97a8 to your computer and use it in GitHub Desktop.
Combine values collected from URL bound to inputs
This file contains hidden or 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: ` | |
<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