考虑这个index.html:
<div id="test-case">
Hello
<button type="button">Press me</button>
</div>
<app> </app>
where app is the root component
。是否可以访问test-case
元素从应用程序组件内部并操纵button click event
通过角度方法?我可以使用普通的 Javascript,但是有没有 Angular 的方法可以做到这一点?
你能得到的最好的就是这样的。不幸的是我们仍然必须使用全局document.querySelector
.
索引.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>App</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<div id="hello">Hello</div>
<pwpl-root></pwpl-root>
</body>
</html>
应用程序组件.ts
import { Component, Renderer2 } from '@angular/core';
@Component({
selector: 'app',
template: ``,
})
export class AppComponent {
constructor(private renderer: Renderer2) {
const hello = document.querySelector('#hello');
this.renderer.listen(hello, 'click', console.log);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)