admin管理员组文章数量:1400182
I have many Angular components; They all have a method with @HostListener And I converted them into web components on @angular/elements and embedded them in some legacy JSP/JSF pages.
This approach works very well and many web components are working properly in those legacy pages.
But now I find one component is not listening the event for @HostListener.
So I tried those:
- Embedded this component into a simple JSP/JSF page and it works
- Embedded another component with @HostListener method into this page and it works
- Try to find big difference between those two components and found nothing
- Debug the code in complicated JSP/JSF page and found the component is created and initialized.
My versions:
"@angular/core": "^15.2.9",
"@angular/elements": "^15.2.9",
A part of my code:
@HostListener('window:onExchangeRateTrigger', ['$event'])
handleExchangeRateTrigger(e: CustomEvent) {
console.log(" never get here");
}
In JSP/JSF side:
function openExchangeRateWarn(ProgramId) {
var event = new CustomEvent("onExchangeRateTrigger", {
detail: {
currencyCode: "CAD",
currencyRate: 0.32,
duration: 8000,
programId: ProgramId,
}
});
window.dispatchEvent(event);
}
I have many Angular components; They all have a method with @HostListener And I converted them into web components on @angular/elements and embedded them in some legacy JSP/JSF pages.
This approach works very well and many web components are working properly in those legacy pages.
But now I find one component is not listening the event for @HostListener.
So I tried those:
- Embedded this component into a simple JSP/JSF page and it works
- Embedded another component with @HostListener method into this page and it works
- Try to find big difference between those two components and found nothing
- Debug the code in complicated JSP/JSF page and found the component is created and initialized.
My versions:
"@angular/core": "^15.2.9",
"@angular/elements": "^15.2.9",
A part of my code:
@HostListener('window:onExchangeRateTrigger', ['$event'])
handleExchangeRateTrigger(e: CustomEvent) {
console.log(" never get here");
}
In JSP/JSF side:
function openExchangeRateWarn(ProgramId) {
var event = new CustomEvent("onExchangeRateTrigger", {
detail: {
currencyCode: "CAD",
currencyRate: 0.32,
duration: 8000,
programId: ProgramId,
}
});
window.dispatchEvent(event);
}
Share
Improve this question
edited Mar 25 at 17:25
Justin Wu
asked Mar 25 at 15:10
Justin WuJustin Wu
113 bronze badges
3
|
1 Answer
Reset to default 0Finally I figured out the root cause:
Some parts of my JSP page are dynamically loaded in the client Side within iFrame , which cut my single JSP page into multiple JS context. The trigger and its related component fell into different JS context.
The fixing is simple, I just put them together.
本文标签: angularHostListener doesn39t work in a complicated pageStack Overflow
版权声明:本文标题:angular - @HostListener doesn't work in a complicated page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744186868a2594330.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ngOnInit() { document.addEventListener("myEvent",() => { console.log('it works'); },false) };
this will confirm whether the hostlistener alone has the problem – Naren Murali Commented Mar 26 at 11:52