I am trying to create a search box that is supposed to do the following when the input value changes or if the user clicks the search icon.
Set the value of
searchText
field. This is used for searching and also, controls which icon (search | clear) to show next to the search input box.Before searching, the value of the field
previewTableLoading
is set to true which is used in the template to display a spinner.After a time-consuming search process, the value of the field
previewTableLoading
is set back to false to stop the spinner.
Here's the template for the search box -
<div class="ui icon input">
<input type="text" (change)="onSearchFieldChange($event)" placeholder="Search Fields..." #searchInput>
<i class="search link icon" (click)="searchInput.change()" *ngIf="!searchText || searchText === ''"></i>
<i class="times link icon" (click)="clearSearch()" *ngIf="searchText && searchText !== ''"></i>
</div>
And here is the implementation code -
onSearchFieldChange(event) {
this.searchText = event.target.value;
this.search();
}
search() {
this.previewTableLoading = true;
// SOME HEAVY SEARCHING ALGORITHM THAT TAKES TIME
this.previewTableLoading = false;
}
clearSearch() {
this.searchText = '';
this.search();
}
PROBLEM:
When the input value changes, the value of the field previewTableLoading
isn't updated instantaneously. It waits for the entire search
method to complete. That is why my spinner which is bound to that field never shows up. I feel like the method call from the template is acting synchronously and all the updates within that method are only coming into effect when the method completes.
I hope I was clear explaining my issue. It'd be great if anyone can help me with understanding the issue. Thanks.