如何从自定义指令发出?

时间:2017-01-23 12:23:25

标签: angular angular2-directives

我一直在研究自定义指令(Scroller),它检测滚动结束。我想在滚动结束时(即到达页面末尾)发出一个数字

自定义指令:

    import { HostListener , Directive, Output, EventEmitter} from '@angular/core';



@Directive({
    selector: '[scroller]'
})


export class scrollDirectiveComponent{
  @HostListener('scroll', ['$event'])

  @Output() emitPageNumber:EventEmitter<any> =new EventEmitter();
  pageNo:any=0;
  onScroll(event) {
    // do tracking
    console.log('scrolled', event.target.scrollTop);
    // Listen to click events in the component
    let tracker = event.target;

    let limit = tracker.scrollHeight - tracker.clientHeight;
    console.log(event.target.scrollTop, limit);
    if (event.target.scrollTop >= limit) {
      console.log('end reached');
      this.pageNo++;
      this.emitPageNumber.emit(this.pageNo);
    }
  }
}

我将指令绑定到我的父模板

        <div class="alert-list" scroller (emitPageNumber)="fetchPageNumber($event)">

但是当我滚动时我得到一个错误  core.umd.js:3462 EXCEPTION:http://localhost:3000/app/menu-board/menu.component.html:63:8中的错误:self._scrollDirectiveComponent_32_3.emitPageNumber不是函数

我做错了什么?

1 个答案:

答案 0 :(得分:2)

@HostListener(...)

后缺少一些代码
@HostListener('scroll', ['$event'])
scrollHandler(event) {
  ...
}