Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Angular2中销毁HostListener_Javascript_Html_Angular_Typescript_Angular2 Template - Fatal编程技术网

Javascript 如何在Angular2中销毁HostListener

Javascript 如何在Angular2中销毁HostListener,javascript,html,angular,typescript,angular2-template,Javascript,Html,Angular,Typescript,Angular2 Template,我正在创建一个页面,当元素在视口中可见时,向下滚动页面时会出现动画,相应的动画也会出现。在使用Angular2时,我想到了使用它编写滚动函数。我找了一整天,发现HostListener可以满足我的要求。但是,我的问题是“使用了多个页面”。因此,我需要滚动功能只出现一个所需的页面。有什么解决办法吗 我还想到一些可能的解决方案,如下所示: 我们可以摧毁听众 为特定页面添加侦听器 如果上面提到的是可能的,那么我们怎么能做到呢 我的代码: import {Component,HostListener}

我正在创建一个页面,当元素在视口中可见时,向下滚动页面时会出现动画,相应的动画也会出现。在使用Angular2时,我想到了使用它编写滚动函数。我找了一整天,发现HostListener可以满足我的要求。但是,我的问题是“使用了多个页面”。因此,我需要滚动功能只出现一个所需的页面。有什么解决办法吗

我还想到一些可能的解决方案,如下所示:

  • 我们可以摧毁听众
  • 为特定页面添加侦听器
  • 如果上面提到的是可能的,那么我们怎么能做到呢

    我的代码:

    import {Component,HostListener} from '@angular/core';
    @Component({
        selector: 'my-app',
        templateUrl:'src/html/home.html',
    })
    
    export class Home {
        @HostListener('window:scroll', ['$event'])
    onScroll(e){
    // My animation code
    }
    }
    
    HTML代码:

    <div (window:resize)="onResize($event)">
    //some code
    </div>
    
    
    //一些代码
    
    我不确定我是否完全理解你的问题。是否要在到达某个滚动点时停止侦听滚动事件?在这种情况下,只需在ngOnInit中创建您自己的侦听器,并在您对这些事件不再感兴趣时在窗口上调用removeEventListener

    import {Component,HostListener} from '@angular/core';
    
    @Component({
        selector: 'my-app',
        templateUrl:'src/html/home.html',
    })
    export class Home {
    
        private boundScrollCallback: Function;
    
        ngOnInit() {
            /**
             * Need to bind the onScroll function so that "this" in
             * onScoll will result in the component instance itself.
             * Otherwise this.removeScrollLiteners() will not work in that context.
             */
            this.boundScrollCallback = this.onScroll.bind(this);
    
            window.addEventListener('scroll', this.boundScrollCallback);
            /**
             * Need this as well as resizing the window may result
             * in a change of scroll position.
             */
            window.addEventListener('resize', this.boundScrollCallback);
        }
    
        onScroll(e){
            if (true /** Logic to check scroll position goes here */) {
                // Animation code
    
                this.removeScrollLiteners(); // Stop listening for events.
            }
        }
    
        /**
         * Remove the event listeners.
         */
        private removeScrollLiteners() {
            window.removeEventListener('scroll', this.boundScrollCallback);
            window.removeEventListener('resize', this.boundScrollCallback);
        }
    
        ngOnDestroy() {
            this.removeScrollLiteners(); // Stop listening for events.
        }
    }
    

    否则我会考虑解决这个问题,因为它使这类事情更容易处理。

    您的意思是使用renderer
    @HostListener
    不能立即删除moment@AngularInDepth.com这有可能很快实现吗?很遗憾。你有什么问题?您不能删除侦听器吗?