Call a method on another page


#1

Hi,

Say I have a page with a public method called foo(). From that page, I navigate to a new page using nav.push(). How would I be able to run foo() from the second page?

You can call methods on components using app.getComponent, but unfortunately, getComponent doesn’t seem to be able to return page components.

So is there any way of doing something like this:
app.getComponents(‘MyFirstPage’).foo() ?


#2

I think you have two options depending on what exactly you want to do.

  1. If you want the method to act locally on it’s own component and just need to call it from elsewhere use the Events in Ionic.

  2. If you want to re-use the code from component A in component B than abstract it to a service and use the service in both components.


#3

Thanks for you reply!

I’d like the method to run it it’s original context. And since a page is really a component as well, it feels like that there is a way to access it just like a component using getComponent.

But events will do the trick if there is no better way, even tho they feel a bit overkill for this.

Thanks again!