I'm trying to dismiss my modal in ionic 2.
I have component which looks like this:
export class DynamicModalComponent<T extends IHasId> {
modalOptions: DynamicModalOptions<T>
constructor(public navCtrl: NavController, public navParams: NavParams) {
this.modalOptions = navParams.get('modalOptions');
}
close() {
this.modalOptions.viewCtrl.dismiss();
}
}
and my html looks like this:
<ion-header>
<ion-navbar align-title="center">
<ion-title> Dynamic Modal Header </ion-title>
<ion-buttons end>
<button (click)="close()" ion-button>
<ion-icon name="save"></ion-icon>
Cancel
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
When I click the cancel button, my function gets triggered but nothing dismisses. I don't think the issue would be in the calling class but just incase ill post the relevant pieces:
constructor(public navCtrl: NavController, public viewCtrl: ViewController, public navParams: NavParams, public baseProvider: UserSkills,
public enumProvider: Enums, public modalCtrl: ModalController, routes: Routes) {
}
showModal(modalOptions: DynamicModalOptions<UserSkill>) {
let modal = this.modalCtrl.create(DynamicModalComponent, { modalOptions });
modal.present();
return modal;
}
onItemClick(entity) {
var modalOptions = this.getDynamicModalOptions(entity);
this.showModal(modalOptions);
}
getDynamicModalOptions(entity: UserSkill): DynamicModalOptions<UserSkill> {
var modalOptions = new DynamicModalOptions<UserSkill>();
modalOptions.entity = entity;
modalOptions.editOptions = this.editOptions;
modalOptions.inputMetadata = this.getInputMetadata();
modalOptions.viewCtrl = this.viewCtrl;
return modalOptions;
}
getDynamicModalOptions
get called this way? I'm not sure of the need to coupleViewController
since you can always inject it in the constructor forDynamicModalComponent
ViewController
. You were passing the instance of theViewController
of the caller, and not theViewController
that represents the modal. Therefore theviewCtrl.dismiss();
didn't do anything (since the modal exists in theDynamicModalComponent
and not the caller component). Another way to fix this kind of issues, is to use the injector (public injector: Injector
) to get the right instance (viewCtrl = this.injector.get(ViewController);
) on theDynamicModalComponent
component