Angular 12.1 使用打字稿添加 html 元素 [英] Angular 12.1 add html element using typescript
本文介绍了Angular 12.1 使用打字稿添加 html 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在通过 youtube 学习 angular,但我正在尝试做一些新的事情,我遇到了一个错误,我的代码附在下面,帮帮我.
我想像这样设置属性 div.setAttribute('(click)',"popUp($event)");
但是我出错了.
打字稿
导出类 AppComponent {创建El(){控制台时间(计时器");for (让 i = 0; i <10; i++) {让 div = document.createElement(div");div.textContent = `你好,世界!${i}`;div.setAttribute('(click)',"popUp($event)");document.getElementById('divEl')?.appendChild(div);};console.timeEnd(计时器");}
HTML
<按钮(点击)=createEl()">点击我</button>
错误
解决方案
下面是正确的检查.
div.addEventListener('click', (e) => {this.popUp(e);});
I'm learning angular via youtube, but I'm trying to do something new, and I'm getting an error on that, my code is attached below, help me out.
I want to setAttribute like this div.setAttribute('(click)',"popUp($event)");
but I got error.
TypeScript
export class AppComponent {
createEl(){
console.time("timer");
for (let i = 0; i < 10; i++) {
let div = document.createElement("div");
div.textContent = `Hello, World! ${i}`;
div.setAttribute('(click)',"popUp($event)");
document.getElementById('divEl')?.appendChild(div);
};
console.timeEnd("timer");
}
HTML
<div id="divEl"></div>
<button (click)="createEl()">click me</button>
Error
解决方案
That's right check below.
div.addEventListener('click', (e) => {
this.popUp(e);
});
这篇关于Angular 12.1 使用打字稿添加 html 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文