var htmlElement: HTMLElement = document.getElementById("ion-searchbar");
htmlElement.nodeValue = '';
and
htmlElement.setAttribute('value', '');
and
htmlElement.setAttribute('ng-reflect-model', '');
But the values does not clear
Because I have done this.searchQuery = '';, when I focus on the search bar, it clears. So this makes me think that the problem is it is not refreshing to show the new value.
In the browser, setting the ngModel value to ‘’ clears the search bar, but running on an Android device the value doesn’t get cleared from the searchbar.
Is there a way to clear the searchbar programmatically?
Changing it through the associated model value - this.searchQuery = ''; - works for me. Are you sure you are within Angular’s context, or are you missing a this.zone.run(() => { ... }); ?
Thanks Michael - not sure what a this.zone,run(() => {…}) I haven’t come across that before?
I did manage to get it to clear on an Android device by setting the “value” property of the control, rather than the ngModel value.
import { Component, ViewChild } from '@angular/core';
export class ScanBarcodePage {
@ViewChild('bluetoothControl') bluetoothControl: Searchbar;
// to set focus on the bluetoothControl and clear the control
this.bluetoothValue = ''; // this clears the model value
this.bluetoothControl.value = ''; // this clears the control value
That’s interesting, glad it works for you. Just so you’re aware of the zone.run issue, which I run into quite often because of third party library callbacks:
If you are familiar with angular 1, then this.zone.run() is the equivalent of $scope.apply(). It’s an execution context that angular is aware of. Example: If you do window.setTimeout(() => { this.foo = 3; });, then {{foo}} will not show up as 3 in the template because angular 2 is not aware of the variable having changed because you switched contexts by using a timeout. Running zone.run() makes sure that angular knows about you running some code that would otherwise be outside of angular’s context.