Angular中如何添加scroll事件

使用 (scroll) 指令

<div class="parent" (scroll)="test($event)" onScroll>
  <div class="child"></div>
</div>

public test($event) {
    console.log('通過(scroll)指令監聽');
}

使用 HostListener

@Directive({
    selector: '[onScroll]',
})
export class OnScrollDirective {
    @HostListener('scroll', ['$event']) public onscroll = ($event) => {
        console.log('通過HostListener監聽');
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章