我正在尝试更新未绑定到<form>组件中的输入的值,而我的研究使我开始使用Renderer2Angular提供的服务。
所以我的输入看起来像这样:
<input type="text" #zipEl placeholder="Enter zip">
<button type="button" (click)="getLocation()">
<span>Use current location</span>
</button>
Run Code Online (Sandbox Code Playgroud)
在我的组件中,我试图像这样简单地设置值:
@ViewChild('zipEl') zipEl:ElementRef;
constructor(private renderer: Renderer2) {
}
getLocation() {
this.renderer.setValue(this.zipEl, '94085');
}
Run Code Online (Sandbox Code Playgroud)
虽然什么都没有发生。当我单击按钮运行该功能时,输入框永远不会更新其值94085。有人知道我在做什么错吗?
提前致谢!
我相信您需要使用setProperty:
getLocation() {
this.renderer.setProperty(this.zipEl.nativeElement, 'value', '94085');
}
Run Code Online (Sandbox Code Playgroud)
小智 0
你的角度版本是什么?
我还没有找到Renderer2在哪个版本
但它可以与 Renderer 一起使用:
@ViewChild('zipEl') zipEl:ElementRef;
constructor(public renderer: Renderer) {
}
getLocation() {
this.renderer.setElementAttribute(this.zipEl.nativeElement, 'value', '94085');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3468 次 |
| 最近记录: |