简体   繁体   English

Ionic 3 / Angular 2 - Renderer2.setValue()不更新输入字段的值

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

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. 我正在尝试更新未绑定到组件中<form>的输入的值,我的研究让我使用Angular提供的Renderer2服务。

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 . 当我单击按钮运行该功能时,输入框永远不会使用94085更新其值。 Does anyone know what I'm doing wrong here? 有谁知道我在这里做错了什么?

Thanks in advance! 提前致谢!

I believe you need to use setProperty : 我相信你需要使用setProperty

getLocation() {
    this.renderer.setProperty(this.zipEl.nativeElement, 'value', '94085');
}

Using Angular v4.0.2, I ended up using the .setAttribute() function shown here: 使用Angular v4.0.2,我最终使用了此处显示的.setAttribute()函数:

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

What is your angular version ? 你的角度版本是多少?

I do not yet find in which version is Renderer2 我还没有找到Renderer2的版本

But it work with Renderer : 但它与Renderer一起使用:

  @ViewChild('zipEl') zipEl:ElementRef;
  constructor(public renderer: Renderer) {
  }


  getLocation() {
    this.renderer.setElementAttribute(this.zipEl.nativeElement, 'value', '94085');
  }

You can try it like this: 你可以这样试试:

  constructor(element: ElementRef, renderer: Renderer2) {
    const input = renderer.createElement('input');
    renderer.setAttribute(input, 'value', '94085');
    renderer.listen(input, 'keyup', (ev) => {
      console.log(ev);
    });
    renderer.appendChild(element.nativeElement, input);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Renderer2.setProperty() 不更新值 - Renderer2.setProperty() doesn't update the value Angular2变化检测问题。 如果类字段没有改变,则输入字段不会更新,但输入字段值不同 - Angular2 change detection issue. Input field doesn't update if class field does'n change, but input field value is different Angular:为什么 <input> 值更改(使用renderer2)不会触发事件绑定到该值 <input> - Angular: why <input> value changes (using renderer2) doesn't trigger the events bind to that <input> Ionic [Angular] - 反应形式:使用 setValue() selectionStart/End 后不会移动 cursor - Ionic [Angular] - Reactive form: after using setValue() selectionStart/End doesn't move cursor 为什么我的 Angular 组件值没有更新? - Why doesn't my Angular component value update? 无法在模糊或setValue上更新绑定值 - Can't update bind value on blur or on setValue Angular FormGroup不会在patchValue或setValue之后立即更新其值 - Angular FormGroup won't update it's value immediately after patchValue or setValue 使用ionic2 / angular 2将所选值附加到输入字段 - Appending the selected value to input field using ionic2 /angular 2 表单中的Angular 5输入不会更新formControl - Angular 5 input in a form doesn't update the formControl 在Angular中使用“ renderer / renderer2”获取组件中DOM输入的值 - Use “renderer/renderer2” in Angular to get value of DOM input in component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM