如何将数据传递给Modal ionic 2

时间:2017-03-09 11:38:59

标签: javascript typescript ionic-framework modal-dialog ionic2

大家好我试图将数据从我的设备视图传递到我的模态(设备详细信息视图)并将其绑定到我的模态,所以如果我(click)=openModal()模式应该打开我点击的参数。但遗憾的是,任何人都知道我该如何处理呢?

//数据来源&模态处理程序

import { Component } from '@angular/core';

import { ModalController, Platform, NavParams, ViewController,NavController } from 'ionic-angular';

import { ModalPage } from '../modal/modal';

@Component({
  selector: 'page-deviceslist',
  templateUrl: 'devicelist.html'
})
export class DevicesListPage {

  devices;
  device;
  constructor(
    public  modalCtrl: ModalController,
    public nav: NavController,
    public params: NavParams,
  ) {

    this.devices = [
      {
        title: 'Küche',
        items: [
          {title: 'KüchenAid', consumption:'32 W', checked:'true'},
          {title: 'Thermomix', consumption:'0 W', checked:'false'}
        ]
      },
      {
        title: 'Wohnzimmer',
        items: [
          {title: 'Fernseher',consumption:'0 W', checked:'false'},
          {title: 'Stehlampe',consumption:'60 W', checked:'true'},
        ]
      }
    ];
      this.device = this.devices[this.params.get('devNum')];
  }

  openModal(deviceNum) {
    let modal = this.modalCtrl.create(ModalPage, deviceNum);
    modal.present();
    console.log(this.device);
      console.log(this.devices);
  }

};

//和我的modal.ts

import { Component } from '@angular/core';
import { ModalController, Platform, NavParams, ViewController } from 'ionic-angular';

@Component({
  selector: 'page-modal',
  templateUrl: 'modal.html'
})
export class ModalPage {

  constructor(
    public platform: Platform,
    public params: NavParams,
    public viewCtrl: ViewController
  ) {
  }

  dismiss(data) {
    this.viewCtrl.dismiss(data);
  }

}

1 个答案:

答案 0 :(得分:51)

使用Ionic v3中的模态控制器传递数据的过程与使用Nav Controller传递数据的过程不同。主要区别在于您使用键和值传递数据。

你应该这样做:

let modal = this.modalCtrl.create(ModalPage, {deviceNum: deviceNum});

和你的模态构造函数:

constructor(public platform: Platform, params: NavParams,public viewCtrl: ViewController) {
    console.log(params.get('deviceNum'));
}