How to call page unloaded event in nativescript angular


#1

I can’t seem to get the unloaded event to fire using my angular app.

<Page xmlns="http://www.nativescript.org/tns.xsd" (unloaded)="onUnloaded">

Is what i have. in my component:

onUnloaded(_args){
        console.log('Navigated to detail');
    }

It’s not getting called.
I do have Page injected into my component if that matters. Anyone know what I need to do? I want to set visibility off an Activity spinner on unload so when navigating back it isn’t spinning.


#2

You would not declare your Page in HTML when using Angular, it’s created and handled internally by page router outlet. The page instance can be injected in your constructor.

Besides, may I know why you may need unloaded event when you have ngDestroy from Angular Component Life Cycle.


#3

Probably because I’m a newbie. I’ve changed it to ngDestroy() but that doesn’t seem to get called when I navigate away. If it is a life cycle event, wouldn’t it get called when navigating away from a page?


#4

OK, I figured it out. ngOnDestroy doesn’t seem to get called, but I can initialize the page on events in the ngOnInit(). So I did this:

ngOnInit() { 
    this.page.on('navigatingTo', (data) => {
        // run init code
        // (note: this will run when you either move forward or back to this page)
        console.log("returned");
      });