Dijish U.K
Dijish U.K

Reputation: 159

How to redirect my ionic 2 app to a new page by using NavController from a provider

Current Scenario --

I have a home page and I call a map service from this page to show a map in my app. This map service uses marker service to display different marker in the map. Now I want to open a new page when user click on the info window of this marker. I have attached event listener for click on marker window as follows and is working fine

marker.addEventListener(GoogleMapsEvent.INFO_CLICK).subscribe(
   data => //Code to navigate to another page,
   err => console.log("some error on marker listener ", err)
);

What I want --

I want to redirect user to a new page from this click listener

What I am doing now --

I have achieved this function by passing the public variable navCtrl from home page to marker service as arguments and calling push function as follow

marker.addEventListener(GoogleMapsEvent.INFO_CLICK).subscribe(
   data => navCtrl.push(OrderPickDetailsPage),
   err => console.log("some error on marker listener ", err)
);

Question -- Is there any better way to do this?

Upvotes: 0

Views: 285

Answers (1)

Vikram Ezhil
Vikram Ezhil

Reputation: 995

The one which you did is the correct and most simplest way of pushing a page when the listener method is triggered.

If the same page is pushed elsewhere in the code instead of rewriting the same push code you could just create a function and put that code inside it so you can reuse it in the class whenever you need it.

private pushOrderPickPage()
{
  this.navCtrl.push(OrderPickDetailsPage);
}

Call this wherever you want in the class,

this.pushOrderPickPage();

Upvotes: 1

Related Questions