用于在 MatDialog 组件中的 Angular 应用程序中实现间谍导航栏。
我实现了一个指令来监视滚动事件,使用
@HostListener('window:scroll', ['$event'])
我也尝试过'scroll'
作为事件名称。但该事件似乎并没有火起来。我尝试了几种方法,例如。 G。通过直接在对话框组件中使用 HostListener 通过使用 JavaScriptwindow.onscroll()
函数和 rxjsfromEvent()
功能但没有成功。
尝试其他 CSS 事件(例如window:click
)工作正常。
我还在一个“正常”组件中尝试过它,该组件不显示为对话框,但该事件也不会在那里触发。
造成这种行为的原因是什么?
这是一个替代方案solution https://stackoverflow.com/questions/44516017/how-to-handle-window-scroll-event-in-angular-4/54005183#54005183效果相当好。
简而言之:
ngOnInit() {
// Add an event listener to window
// Window can be defined in the pollyfiles.ts as:
// if (window) {
// (window as any).global = window;
// }
window.addEventListener('scroll', this.scroll, true); //third parameter
}
ngOnDestroy() {
window.removeEventListener('scroll', this.scroll, true);
}
scroll = (event: any): void => {
// Here scroll is a variable holding the anonymous function
// this allows scroll to be assigned to the event during onInit
// and removed onDestroy
// To see what changed:
const number = event.srcElement.scrollTop;
console.log(event);
console.log('I am scrolling ' + number);
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)