[英]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.