【发布时间】:2016-11-03 10:57:53
【问题描述】:
我需要在页面加载时更改输入的值。但是,我无法做到。这就是我在单独的 html 文件中创建输入的方式。
<form (submit)="some_function()">
<input (keypress)="some_function()" name="test" id="test-input" [(ngModel)]="test" [placeholder]="'NEED TO CHANGE INPUT TEXT' | translate"/>
<button type="submit">Submit </button>
然后在一个 ts 文件中我有这样的东西:
ngOnInit() {
var element = document.getElementById("test-input");
// If I console.log(element) I actually get it.
element.textContent = "INPUT TEXT CHANGED??";
// If I console.log(element.textContent) I get the input with changed text... but I can't see it otherwise
}
我在这里做错了什么?有什么帮助吗?
【问题讨论】:
-
您可以更改
ngOnInit中的输入值,就像您使用ngModel和输入一样:this.test = "new value"