V2 - Multiple pages pushed on modal, dismissing viewController errors out
Short description of the problem:
Create a Page (PageOne.js) and add it to a navigation controller. Display a Modal with a page (PageTwo.js). From the modal page (PageTwo.js), push another page (PageThree.js). Attempt to close the modal from the page (PageThree) via the injected viewController's dismiss method.
What behavior are you expecting?
I would expect the modal to dismiss and all of the pages pushed onto the view controller from the modal would be popped.
Steps to reproduce:
- Create a Page (PageOne.js) and add it to a navigation controller.
- Display a Modal with a page (PageTwo.js)
- From the modal page (PageTwo.js), push another page (PageThree.js)
- Attempt to close the modal from the Page (PageThree).js via the injected ViewController instance's dismiss method
Ionic Version: 1.x / 2.x
2.x
Browser & Operating System: iOS / Android / Chrome
iOS, Android, Chrome
Run ionic info from terminal/cmd prompt:
Your system information:
Cordova CLI: 6.0.0
Gulp version: CLI version 3.9.0
Gulp local:
Ionic Version: 2.0.0-beta.1
Ionic CLI Version: 2.0.0-beta.17
Ionic App Lib Version: 2.0.0-beta.8
ios-deploy version: 1.8.3
ios-sim version: 5.0.4
OS: Mac OS X El Capitan
Node Version: v4.2.3
Xcode version: Xcode 7.2.1 Build version 7C1002
The ViewController you injected on PageThree is the view controller for that page, not the entire modal. So when you dismiss PageThree's view controller, you're dismissing just page three. Is that not working like how I described, or you think it should work differently?
I think it should behave differently. It seems kind of weird to me. The use case is having a form in a modal. One of the fields could get complicated, so I have an ion-item that links to another page with the complex form. I would expect to be able to hit an X button in the top right corner of any of the pages in the modal and close it.
So, would you imagine passing the viewController to the next viewController pushed onto the stack via the navParams? That is doable but it feels a little funny to me.
Is a better option to create a new NavController in the modal?
I should note it's not only a question of behavior. There is an exception that freezes the app.
TypeError: Cannot read property 'direction' of null
opts.direction = opts.direction || 'back';
opts is null in the NavController.remove method, so opts.direction has a null pointer.
Worth noting:
In the modal, if you push another page on, then hit the back button (so at the root of the modal), the back button appears. I would not expect this behavior because I am in a modal which implies a different context. The back button does not appear when initially presenting the modal, only when navigating to a new page and returning.
Sorry, but the ViewController represents the actual page it is on, not the parent modal. Having the viewcontroller try to guess which view ionic thinks you want to close would cause more issues and be problematic. I'd recommend instead using NavController and finding the nav that contains the modal, and update that instead.
I am totally fine with that. Is the suggestion to use the NavController and pop the top view when navigating away from a page, then? That should be okay, but how do we get data back to the child unless we pass the parent to the child via NavParams, or use a shared singleton?
Hi, danbucholtz
I have the same problem with u, I want to dismiss the modal after a push(PageThree), but failed and back to PageTwo
Have u fixed this problem?
在Ionci2中实现NavController与ModalController弹出多个窗体传值并返回问题 let firstViewCtrl = this.navCtrl.first(); this.navCtrl.remove(firstViewCtrl.index, this.navCtrl.length()).then(()=>{ let data = { 'foo': 'Test1:从test3返回了' }; firstViewCtrl.dismiss(data); });
It might relate. I found problem in the following scenario in which leads me to learn a hard lesson.
I show a modal on current page. Then push a new page on top of modal. Now I want to navigate back to the root page (the page that showed a modal). What I found is that, I just can't use this.navCtrl of modal with an intention to go back to the first page. It's because of NavController of modal and page are not the same object, it manages its own stack.
Thus if I need to manage navigation on behalf of modal, I need to send in NavController of the page to modal then manage thing further on that.
@haxpor Good explanation, as far as I can tell this is how the navigation stack is supposed to work.
@six006 Thank man!! 赞 I have tested the code suggested by @six006 . Work like a charm!!
let firstViewCtrl = this.navCtrl.first();
this.navCtrl.remove(firstViewCtrl.index, this.navCtrl.length()).then(() => {
firstViewCtrl.dismiss();
});
the navCtrl.remove() is necessary, otherwise the pageThree will remain on screen after the modal dismissed.
A better way to achieve multi-page modals, could be to wrap the pages inside an ion-nav. Which allows you to dismiss it from any page in the stack. So the modal parent would look like:
@Component({
selector: 'multipage-modal-parent',
template: `
<ion-nav [root]="root"></ion-nav>
`
})
export class MultipageModalComponent{
constructor(viewCtrl: ViewController) {}
root = FirstPageComponent;
}
Then from any component inside the ion-nav:
this.navCtrl.parent.getActive().dismiss();
Where getActive() gives you the viewController to call dismiss() on.
I have other way to do that:
let modal = this.modalCtrl.create(SimpleModalPage);
modal.present()
modal.onDidDismiss((result) => {
let nav = modal.getNav();
nav.popAll();
// do something with result
});
page in modal:
closeModal(result) {
this.navCtrl.first().dismiss(result);
}
By this way you can dismiss any page more convenient in modal. It is better that Ionic provide a method for close modal in modal instance :).
this was not animating like a modal pop should, so this is what I've come up with:
this.navCtrl.popToRoot({animate: false}).then(() => firstNavCtrl.dismiss())
Just to make it clear for everyone else having the same problem, the final code that works for me is as follows (combined all the answers above)
let firstViewCtrl = this.navCtrl.first();
this.navCtrl.popToRoot({animate: false}).then(() => firstViewCtrl.dismiss());
For those landing here following the demo in the Modal docs, the following code works:
constructor(private navCtrl: NavController){}
this.navCtrl.rootParams.viewCtrl.dismiss();
rootParams will show up as an error since it's not defined on navController but it compiles.
@rsmn
let firstViewCtrl = this.navCtrl.first(); this.navCtrl.popToRoot({animate: false}).then(() => firstViewCtrl.dismiss());
ERROR Error: Uncaught (in promise): removeView was not found
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.