ionic ion-searchbar 組件獲取焦點方法:
import {Directive, Renderer, ElementRef} from "angular2/core";
@Directive({
selector : '[focuser]'
})
export default class Focuser {
constructor(public renderer: Renderer, public elementRef: ElementRef) {}
ngOnInit() {
//search bar is wrapped with a div so we get the child input
const searchInput = this.elementRef.nativeElement.querySelector('input');
setTimeout(() => {
//delay required or ionic styling gets finicky
this.renderer.invokeElementMethod(searchInput, 'focus', []);
}, 0);
}
}
<ion-searchbar [focuser] placeholder="Enter Your Address"></ion-searchbar>