Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.8k views
in Technique[技术] by (71.8m points)

angular - Ionic 5 pass value from Modal to Page

I need to pass a data (just one string) from a modal to his "parent page" on Ionic5, basically when the user click on an item of the Modal it should pass the string to the page which filter in an array and show the result

Those are the clickable items on the modal:

<ion-row>
  <ion-col class="ion-no-padding-vertical">
    <ion-list class="ion-no-padding-vertical">
        <ion-item class="ion-no-border" button>
          <ion-icon class="ion-input-icon-big" slot="start" name="bu"></ion-icon>
          <ion-label>Burn</ion-label>
        </ion-item>
    </ion-list>
  </ion-col>
</ion-row>

<ion-row>
  <ion-col class="ion-no-padding-vertical">
    <ion-list class="ion-no-padding-vertical">
        <ion-item class="ion-no-border" button>
          <ion-icon class="ion-input-icon-big" slot="start" name="be-battery-weak"></ion-icon>
          <ion-label>Battery Too Weak</ion-label>
        </ion-item>
    </ion-list>
  </ion-col>
</ion-row>

This is the page.ts of the modal:

import { Component, OnInit } from '@angular/core';
import { ModalController } from '@ionic/angular';
@Component({
  selector: 'app-cmd-v4-modal',
  templateUrl: './cmd-v4-modal.page.html',
  styleUrls: ['./cmd-v4-modal.page.scss'],
})
export class CmdV4ModalPage implements OnInit {
 constructor(private modalController: ModalController) { }
 ngOnInit() { }
 async closeModal() {
     await this.modalController.dismiss();
 }
}

and this is the page.ts of the "parent" page:

import { Component, OnInit } from '@angular/core';
import { Router, NavigationExtras } from '@angular/router';
import { ModalController } from '@ionic/angular';

import { CmdV4ModalPage } from './cmd-v4-modal/cmd-v4-modal.page';
import * as cmd_v4_error_json from '../../assets/ErrorCodes/CMD-V4-ERROR.json';

@Component({
  selector: 'app-cmd-v4',
  templateUrl: './cmd-v4.page.html',
  styleUrls: ['./cmd-v4.page.scss'],
})
export class CmdV4Page implements OnInit {

  constructor(private modalController: ModalController) { }

  async CMDV4Modal() {
    const modal = await this.modalController.create({
      component: CmdV4ModalPage
    });
    return await modal.present();
  }

  ngOnInit() { }
}

I tried to Google but I don't find a way to achieve this. Thanks

REPLY to Peter

thank you so much for the help but I just have an error:

error TS2554: Expected 0 arguments, but got 1.

got this on modal.page.ts

async closeModal() {
let data = { name : 'John'};
this.modalController.dismiss(data);
}

and this on the main page cmd-v4.ts (parent page)

async CMDV4Modal() {
const modal = await this.modalController.create({
component: CmdV4ModalPage
});
modal.onDidDismiss(data => {
console.log(data);
});
return await modal.present();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Inside the dismiss() method pass the string:

let data = { name : 'John'};
this.modalController.dismiss(data);

Then in your page, you can use onDidDismiss() to get the value from the modal:

  async CMDV4Modal() {
    const modal = await this.modalController.create({
      component: CmdV4ModalPage
    });
    modal.onDidDismiss(data => {
     console.log(data);
   });
   await modal.present();
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...