Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 3/Angular 2 - Renderer2.setValue() doesn't update the value of my input field

Tags:

angular

ionic2

I'm trying to update the value of an input that isn't bound to a <form> in my component, and my research has lead me to use the Renderer2 service that Angular provides.

So I have my input that looks like this:

<input type="text" #zipEl placeholder="Enter zip">
<button type="button" (click)="getLocation()">
   <span>Use current location</span>
</button>

And in my component, I'm trying to just set the value simply like this:

@ViewChild('zipEl') zipEl:ElementRef;

constructor(private renderer: Renderer2) {

}

getLocation() {
    this.renderer.setValue(this.zipEl, '94085');
}

Though nothing is happening. When I click the button to run the function, the input box never gets its value updated with 94085. Does anyone know what I'm doing wrong here?

Thanks in advance!

like image 290
Stevie Star Avatar asked Dec 02 '22 11:12

Stevie Star


2 Answers

I believe you need to use setProperty:

getLocation() {
    this.renderer.setProperty(this.zipEl.nativeElement, 'value', '94085');
}
like image 162
Miller Avatar answered Dec 04 '22 01:12

Miller


Using Angular v4.0.2, I ended up using the .setAttribute() function shown here:

getLocation() {
    this.renderer.setAttribute(this.zipEl, 'value', '94085');
}
like image 29
Stevie Star Avatar answered Dec 03 '22 23:12

Stevie Star