Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the wrapped input from an ion-input in Ionic2?

Tags:

I'd like to attach an google.maps.places.Autocomplete to an ion-input, but in Ionic2 ion-input wraps the <input> element and I can't figure out how to get access to it.

I've tried creating a directive and using the passed in ElementRef

import {Directive, ElementRef, Input} from 'angular2/core';

@Directive({
    selector: '[location-picker]'
})

export class LocationPicker {
    constructor(el: ElementRef) {    
        console.log(el.nativeElement);    
    }
}

but nativeElement returns the wrapped input.

<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>

I've also tried creating a custom component similar to this and switching Searchbar to TextInput, but TextInput doesn't have.inputElement`.

Any ideas would be welcome.

Thanks!

like image 275
cnorris Avatar asked May 01 '16 15:05

cnorris


People also ask

How do you find an ionic input?

Ionic still uses an actual <input type="text"> HTML element within the component, however, with Ionic wrapping the native HTML input element it's better able to handle the user experience and interactivity. Similarly, <ion-textarea> should be used in place of <textarea> .

How do you set the value of ion input?

By using [(ngModel)]="value" you bind the value variable to that input field. That way it is always updated. If you change it in the UI, it is immediately updated "in the code". And when you update it in your code, it automatically updates in the UI.

How do you make ion input readonly?

Add an ion input tag with conditional readonly <ion-input [readonly]="isReadonly"> Set isReadonly variable to true. readonly attribute is not rendered on the resulting input tag.


1 Answers

Not sure this is what you're looking for (don't know 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);
}

See also angular 2 / typescript : get hold of an element in the template

like image 193
Günter Zöchbauer Avatar answered Oct 02 '22 04:10

Günter Zöchbauer