Reputation: 655
I am trying to build an app, which has the following work flow.
From login page when user have given then correct credentials i am calling a service.If it is success,i am making Homepage as the root of nav controller.
if(getLoginDetails.status == "success")
{
this.navCtrl.setRoot(HomePage);
}
but still when i used this.navCtrl.length()
in HomePage constructor,its show 2.How can this happen,when i just replaced root element of navigation stack from LoginPage with homepage.As per my understanding,the count should come as 1.
Also except login each page has swipeEnabled = true
menu and a back button.
Now suppose user go to HomePage and then createRequestPage and then used menu to go to requestListPage,now i need that when user click back button it should take user to homePage.For this my approach is that when user click through menu,i will remove all pages in navigation stack except the first one that is our root-Homepage.
I tried with this way but its not working-
this.navCtrl.remove(1,this.navCtrl.length(),{}).then(
()=>{
this.navCtrl.push(RequestListPage)
});
But this does not seems to work. Can somebody help me with this. Also some better docs for NavController of ionic2.
Upvotes: 0
Views: 421
Reputation: 1093
if you put navCtrl in the provider that is the way:
In provider import
import { NavController, App } from 'ionic-angular';
in export class init
private navCtrl: NavController;
in constructor
constructor(private app:App) {
this.navCtrl = app.getActiveNav();
}
Define a function go to home page:
goToHomePage() {
this.navCtrl.setRoot(HomePage);
}
Now can call this.navCtrl in any page you like
import { YourProvider } from "../../your-provider";
@Component({
selector: 'page',
templateUrl: 'page.html',
providers: [YourProvider]
})
export class YourPage{
constructor(private yourProvider : YourProvider) {
}
goToHome() {
this.yourProvider.goToHomePage();
}
now you can use navCtrl
Goodluck
Upvotes: 0
Reputation: 7719
Do you have the selector
tag set in your @Component
? This is a little bug in Ionic, which causes the page to push itself onto the stack, this would explain why the length is 2 and why you have a back button.
If this is the case, removing the selector
should do the trick.
And why not just switch over a boolean if the person came through the menu or not? Then just push to the HomePage if that boolean is true. Simpler and probably cleaner in code as well.
Upvotes: 0