【问题标题】:How can I get the wrapped input from an ion-input in Ionic2?如何从 Ionic2 中的离子输入中获取包装输入?
【发布时间】:2017-04-27 05:57:06
【问题描述】:

我想将 google.maps.places.Autocomplete 附加到 ion-input,但在 Ionic2 ion-input 包装了 <input> 元素,我不知道如何访问它。

我尝试创建一个指令并使用传入的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>

我也尝试过创建类似于this 的自定义组件并将Searchbar 切换为TextInput,但TextInput doesn't have.inputElement`。

欢迎提出任何想法。

谢谢!

【问题讨论】:

  • 嘿,您能否提供完整的代码,说明您如何使用 ionic 2 实现 google 地方自动完成功能? :)。谢谢

标签: ionic-framework angular ionic2


【解决方案1】:

不确定这是您要找的东西(不知道 Ionic)

<ion-input location-picker="">
    <input #myInput 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>
@ViewChild('myInput') input; 

ngAfterViewInit() {
  console.log(this.input.nativeElement.value);
}

另见angular 2 / typescript : get hold of an element in the template

【讨论】:

  • 不幸的是,这只是console.log(el.nativeElement); 的输出,所以我无法以这种方式对其进行编辑。我已经编辑了我的问题以使其更清楚。不过谢谢!
  • this.nativeElement.querySelector('input') 呢?
  • 可能需要移动到 ngAfterViewInit() 以给 Angular 时间来完成构建视图。
【解决方案2】:

有同样的问题。接受的答案和它下面的 cmets 的组合似乎为我解决了这个问题。

将此功能添加到我的指令中似乎可以做到。我正在使用打字稿和 谷歌地图的类型定义在这里找到:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/googlemaps/google.maps.d.ts

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);
    });
  }

【讨论】:

    【解决方案3】:

    我是这样解决的:

    <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();
       });
    }
    

    【讨论】:

    • 仍然没有弄清楚如何将此代码移植到指令中。如果您有任何建议,请告诉我!
    猜你喜欢
    • 1970-01-01
    • 2017-09-30
    • 2016-11-08
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 2018-06-23
    相关资源
    最近更新 更多