Marc-André Therrien Marc-André Therrien - 5 months ago 50
AngularJS Question

Angular 2 ngModel value not updating fast enough using ionic

I have a

ion-searchbar
that looks like this

<ion-searchbar [(ngModel)]="searchQuery" (keyup.enter)="search();"></ion-searchbar>


and the
searchQuery
is defined in my typescript file like this

export class SearchPage {
searchQuery: string;

constructor(){}

search() {
this.service.search(this.searchQuery).subscribe(
data => this.searchResults = data,
error => {
//something
}, () => {
//something
}
);
}
}


The problem is that if I change the value too fast and I press Enter, the value of
searchQuery
is not updated. For example, if I search "test" and I wait two seconds it will work. If I then search "testing" and I type it fast and press Enter right away, the value will still be "test". Now, I know this sounds weird, but it is really happening!

Any ideas why the value is not changed as soon as I type something?

Thank you

Answer

I found the solution in the link given by mayur. I replaced the ngModel and the keyup.enter event by (ionInput)="search($event)". In my typescript file, I get the search value like this

search(event) { var searchQuery = event.target.value; //do something }