我有这个代码,我想动态创建 id 并获取它以便能够通过单击来更改其内容
<div class="row" *ngFor="let row of [1, 2, 3]">
<button (click)="mycat(row, col)" *ngFor="let col of [1, 2, 3]" class="square">{{enter.zero}}</button>
</div>
还有我的 Ts
public mycat(myrow, mycol){
console.log('hello my friend');
console.log(myrow);
console.log(mycol);
}
如果使用 javascript,我会使用DocumentGEtElementByID或 Jquery $("#myid"),但在 Angular 中我不知道
我需要当你点击按钮时,改变被点击的按钮的内容,我知道你必须输入一个 id #
我想我有一个解决方案。您可以使用document.getElementById
和HTMLInputElement
内容来做到这一点。
使用 id 获取价值
onClick() {
let outputValue = (<HTMLInputElement>document.getElementById('yourID'));
console.log(outputValue.value);
}
使用 id 设置值
onClick() {
let inputValue = (<HTMLInputElement>document.getElementById('yourID'));
inputValue.value="SS-Data Set Done";
}
注意:这是在 stackblitz 中提供的演示代码。
请检查链接Stackblitz 演示链接。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句