简体   繁体   English

是否有可能在简单服务中没有组件@或Angular @HostListener('window:scroll',)

[英]Is it possible to have Angular @HostListener('window:scroll',) in simple Service not Component or Directive

Is it possible to have Angular @HostListener('window:scroll',) in simple Service not Component or Directive code? 是否有可能在简单Service中没有组件@或指令代码中的Angular @HostListener('window:scroll',)?

I don't want to polute any of my components, since the awareness of scroll should be injected in several other services... Currently i have following code that compiles, but does not work somehow. 我不想污染我的任何组件,因为应该在其他几个服务中注入对滚动的了解……目前,我拥有以下可编译的代码,但无法正常工作。

import { Component, OnInit, Injectable, Inject, HostListener } from '@angular/core';
import { SearchService } from './search.service';
import { DOCUMENT } from '@angular/platform-browser';

const scrollStepToReload = 3700;

@Injectable()
export class ScrollWatcherService {

  private maxReachedScroll = 0;
  private lastLoadedAt = 0;

  constructor(private searchService: SearchService, @Inject(DOCUMENT) private document: any) { }

  @HostListener('window:scroll', ['$event'])
  onScroll($event) {

    const scrollOffset = window.pageYOffset || this.document.documentElement.scrollTop || this.document.body.scrollTop || 0;
    console.debug("Scroll Event", scrollOffset);

    if (scrollOffset > this.maxReachedScroll) {
      this.maxReachedScroll = scrollOffset;
    }
    if (this.lastLoadedAt + scrollStepToReload < this.maxReachedScroll) {

      this.lastLoadedAt = this.maxReachedScroll;
      this.searchService.continueSearch();

    }
  }
}

Similar code works i Component as desired. 类似的代码可以根据需要在Component上运行。 Any ideas how to get it run in a service? 任何想法如何使其在服务中运行? or something similar? 或类似的东西?

You cannot have a HostListener in a service. 服务中不能包含HostListener。 Use the classic: 使用经典:

window.addEventListener('scroll', () => { console.log('scrolling'); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 8 @HostListener(&#39;window:scroll&#39;, []) 不起作用 - Angular 8 @HostListener('window:scroll', []) not working Angular 2+ 使用 HostListener 或 window.pageYOffset 滚动? - Angular 2+ scroll with HostListener or window.pageYOffset? 在Angular2的指令或组件中将@HostBinding和@HostListener作为条件 - Make @HostBinding and @HostListener conditional in a Directive or Component for Angular2 Angular 4 @HostListener窗口滚动事件奇怪地在Firefox中不起作用 - Angular 4 @HostListener Window scroll event strangely does not work in Firefox Angular - Hostlistener 窗口:页面重新加载后不调用滚动函数 - Angular - Hostlistener window:scroll function does not called after page reload 角度 2 中的组件特定 HostListener - Component specific HostListener in angular 2 是否可以在服务中使用 HostListener? 或者如何在 Angular 服务中使用 DOM 事件? - Is it possible to use HostListener in a Service? Or how to use DOM events in an Angular service? Angular - HostListener 指令和传递值 - Angular - HostListener directive and pass value Angular 2指令中的@HostListener不允许操纵元素 - @HostListener in Angular 2 directive is not allowing to manipulate elements 在 angular 指令中使用 @HostListener 的 mouseleave 不会始终触发 - mouseleave with @HostListener not firing always in angular directive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM