Ionic framework 如何从Ionic2中的离子输入获取包装输入?
我想将Ionic framework 如何从Ionic2中的离子输入获取包装输入?,ionic-framework,angular,ionic2,Ionic Framework,Angular,Ionic2,我想将google.maps.places.Autocomplete附加到离子输入,但在离子输入中封装了元素,我不知道如何访问它 我尝试创建一个指令并使用传入的ElementRef import {Directive, ElementRef, Input} from 'angular2/core'; @Directive({ selector: '[location-picker]' }) export class LocationPicker { constructor(e
google.maps.places.Autocomplete
附加到离子输入
,但在离子输入中封装了
元素,我不知道如何访问它
我尝试创建一个指令并使用传入的ElementRef
import {Directive, ElementRef, Input} from 'angular2/core';
@Directive({
selector: '[location-picker]'
})
export class LocationPicker {
constructor(el: ElementRef) {
console.log(el.nativeElement);
}
}
但是nativeElement
返回包装的输入
<ion-input location-picker="">
<input class="text-input ng-valid ng-dirty ng-touched" type="text" placeholder="" aria-labelledby="lbl-6" location-picker="" autocomplete="off" autocorrect="off">
<!--template bindings={}-->
<!--template bindings={}-->
<!--template bindings={}-->
</ion-input>
我还尝试创建一个类似于的自定义组件,并将搜索栏
切换到文本输入
,但文本输入没有
.inputElement`
任何想法都欢迎
谢谢 不确定这是您要找的(不知道)
另见有同样的问题。被接受的答案和下面的评论相结合,似乎解决了我的问题
将此函数添加到我的指令中似乎可以做到这一点。我正在使用打字脚本和
谷歌地图的打字定义如下:
ngAfterViewInit(){
变量输入:HTMLInputElement=this._el.querySelector(“输入”);
this.autocomplete=new google.maps.places.autocomplete(输入,{});
google.maps.event.addListener(this.autocomplete,'place\u changed',()=>{
var place:google.maps.places.PlaceResult=this.autocomplete.getPlace();
本.调用事件(地点);
});
}
我是这样解决的:
<ion-item>
<ion-label>Search your city</ion-label>
<ion-input formControlName="placeAutofill" id="googlePlaces" required></ion-input>
</ion-item>
不幸的是,这只是console.log(el.nativeElement)的输出代码>所以我不能用那种方式编辑它。我对我的问题进行了编辑,使之更清楚。谢谢你!关于this.nativeElement.querySelector('input')
?可能需要移动到ngAfterViewInit()
,以便有时间完成视图的构建。嘿,您是否能够提供有关如何使用ionic 2实现google places Autocomplete的完整代码?:)。Thankstill还没有弄明白如何将此代码移植到指令。如果你有什么建议,请告诉我!
@ViewChild('myInput') input;
ngAfterViewInit() {
console.log(this.input.nativeElement.value);
}
ngAfterViewInit() {
var input: HTMLInputElement = <HTMLInputElement>this._el.querySelector("input");
this.autocomplete = new google.maps.places.Autocomplete(input, {});
google.maps.event.addListener(this.autocomplete, 'place_changed', () => {
var place: google.maps.places.PlaceResult = this.autocomplete.getPlace();
this.invokeEvent(place);
});
}
<ion-item>
<ion-label>Search your city</ion-label>
<ion-input formControlName="placeAutofill" id="googlePlaces" required></ion-input>
</ion-item>
ionViewWillEnter() {
// Google Places API auto complete
let input = document.getElementById('googlePlaces').getElementsByTagName('input')[0];
let autocomplete = new google.maps.places.Autocomplete(input, {types: ['geocode']});
google.maps.event.addListener(autocomplete, 'place_changed', () => {
// retrieve the place object for your use
let place = autocomplete.getPlace();
});
}