从模态返回Ionic 4 Pass数据未定义

时间:2019-03-01 19:42:46

标签: angular modalviewcontroller ionic4

我正在尝试创建一个模态窗口,当用户从该单选组中选择一个值时,将其传递给单选组,然后让我的模态回传他们选择的对象。

广播组页面和html

  export class PopoverstationsPage implements OnInit {

      cities :any

      constructor(public nav : NavController,public nav : NavController,public modalController: ModalController ,public router : Route) { }

      ngOnInit() {
        console.log(this.cities)
      }

      onRadioChange(cities: PopoverstationsPage):void {
    this.modalController.dismiss(cities);

      console.log(this.cities)
  }

    }

html

<ion-list>
        <ion-radio-group (ionChange)="onRadioChange()" [(ngModel)]="cities">
            <ion-list-header>
                <ion-label>Name</ion-label>
            </ion-list-header>

            <ion-item>
                <ion-label>ميناء خصب</ion-label>
                <ion-radio slot="start" value="26.2105,56.244" ></ion-radio>
            </ion-item>

            <ion-item>
                <ion-label>مطار خصب</ion-label>
                <ion-radio slot="start" value="26.161722,56.23672"></ion-radio>
            </ion-item>
            <ion-item>
                <ion-label>دبا</ion-label>
                <ion-radio slot="start" value="25.615627,56.247322"></ion-radio>
            </ion-item>
            <ion-item>
                <ion-label>البريمي</ion-label>
                <ion-radio slot="start" value="24.233913,55.916176"></ion-radio>
            </ion-item>

        </ion-radio-group>
  </ion-list>

上面的主页接收值表单页面,然后在另一个函数中使用它

     export class HomePage implements OnInit {

              cities :any

              constructor(public nav : NavController,public modalController: ModalController) { }

              ngOnInit() {
                console.log(this.cities)
              }

            async openUserModal() {
           const modal = await this.modalController.create({
         component: PopoverstationsPage,
        componentProps: { cities: this.cities },
      });

      modal.onDidDismiss()
        .then((data) => {
          const user = data['cities']; // Here's your selected user!
          console.log(user)
      });

      return await modal.present();
    }


async test (){

  this.https.get('weather/'+this.cities+'.json')
  .subscribe(data => {

    this.weather = data

  })


}

 }

控制台日志undefined中出现错误

2 个答案:

答案 0 :(得分:0)

normal.dot处理程序内部,您正在注销onDidDismiss,但要在模式中返回data['cities']。这意味着您正在查询类型为String的类型cities的属性String,这就是为什么它返回Object并且不抛出错误的原因。应该通过下面的方法解决。

undefined

答案 1 :(得分:0)

此代码适用于离子4。

 async presentModal() {
    let showModal = await this.modalCtrl.create({
      component: ModalPagePage,
      componentProps: { selectedDay : this.selectedDay }
    });
  await showModal.present();

  const { data } = await showModal.onDidDismiss();
  console.log("data is  "+ data);
  if (data) {
        let eventData = data;

      }
    }