简体   繁体   中英

How to destroy HostListener in Angular2

I am creating a page in which animation will occur on scrolling down the page, when the element is visible on the viewport its corresponding animation will occur. As I'm using Angular2, thought of writing scroll function using it. I searched the whole day and found that HostListener will satisfy what I looked for. But, my problem is "Multiple pages have been used". Hence I need the scroll function to occur only one the required page. Is there any solution for this.

I also think of some possible solution as listed:

  1. We can destroy the Listener
  2. Adding a Listener for a particular page

If the above mentioned are possible then how could we do that.

My Code:

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 Code:

<div (window:resize)="onResize($event)">
//some code
</div>

I'm not sure I understand your question completely. Do you want to stop listening for the scroll-event whenever you've reached a certain scroll point? In that case, just create your own listener in ngOnInit and call removeEventListener on window when you're no longer interested in these events.

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.
    }
}

I would otherwise look at IntersectionObserver to solve this problem since it makes these kinds of things easier to deal with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM