I have my search implemented in Angular using async pipe with an observable. I assign the observable to the search observable done with switchmap. But it does not work! I think it has something to do with one assigning is overwriting the other one. But I need to have some data show before beginning to search (who doesnt anyway.) I tried to reverse it by assigning the data first and then the search but then no data is shown and it only works when I begin typing.
So my question is how can I fix it so I can have the search feature and still display some data at the beginning. Using async pipe and not subscribing in component
And the scenario to make it clearer > At component initialization I want to make a request to server and get initial data display it. And also 'assigning' my same observable to the input where the user types criteria to search, by making another request to the server.
Any help is appreciated
Similar question but no concrete answer and deprecated rxjs operators
Angular2 - get data from service on init with Switchmap
queryField: FormControl;
customerOrdersObservable: Observable<NextCustomerOrdersPaginationModel>;
ngOnInit() {
this.customerOrdersObservable = this.queryField.valueChanges.pipe(
debounceTime(500),
distinctUntilChanged(),
switchMap((searchInput) => {
this.transformSearchInput(searchInput);
return this.customerOrderService.getSearchedCustomerOrders(this.page, this.pageSize, this.searchTerm);
}));
this.customerOrdersObservable = this.customerOrderService.getSearchedCustomerOrders(
this.page, this.pageSize, this.searchTerm);
}
Edit to
ngOnInit() {
this.customerOrdersObservable = this.queryField.valueChanges.pipe(
startWith(this.customerOrderService.getSearchedCustomerOrders(
this.page, this.pageSize, this.searchTerm)), > 'would have been great but its not possible (unless im wrong)'
debounceTime(500),
distinctUntilChanged(),
switchMap((searchInput) => {
this.transformSearchInput(searchInput);
return this.customerOrderService.getSearchedCustomerOrders(this.page, this.pageSize, this.searchTerm);
}));
}