假设我有两个组件:
家长模板:
<div #scrollable style="overflow: scroll">
<ng-content></ng-content>
</div>
Run Code Online (Sandbox Code Playgroud)
用例:
<parent>
<child></child>
</parent>
Run Code Online (Sandbox Code Playgroud)
什么是“有角度的”、分离的侦听divscroll事件#scrollable但在<child></child>组件内部的方式?
AFAIK@HostListener将无法定位#scrollable.
谢谢
如何使用带有 fromEvent 的组件获取滚动位置?
我用它来获取鼠标位置,如何从顶部位置滚动?
this.mouseMoveSubscription = fromEvent(this.elementRef.nativeElement, 'mousemove')
.subscribe((e: MouseEvent) => {...});
Run Code Online (Sandbox Code Playgroud)
这不起作用:
ngOnInit() {
this.scrollSubscription = fromEvent(this.elementRef.nativeElement, 'scroll')
.subscribe((e: Scroll) => {
console.log(e.position);
});
}
Run Code Online (Sandbox Code Playgroud)
也没有为此触发任何事件(来自:answer):
@HostListener('scroll', ['$event']) // for scroll events of the current element
onScroll(event) {
...
}
Run Code Online (Sandbox Code Playgroud) angular ×2