14

In Ionic 4, I am trying to use the modalController to open a modal. I am able to open the modal and send componentProps, but I'm not sure how to receive those properties.

Here's how I open the modal component:

async showUpsert() {
  this.modal = await this.modalController.create({
    component:UpsertComponent,
    componentProps: {test: "123"}
  });
  return await this.modal.present();
}

My question is; in the actual modal, how do I get test: "123" into a variable?

4

3 回答 3

28

您可以在需要的组件中使用 Input Component Interaction 获取这些值,例如:

import { Component } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { TestComponent } from '../test/test.component';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss']
})
export class HomePage {
  constructor(public modalController: ModalController){}
  async presentModal() {
    const modal = await this.modalController.create({
      component: TestComponent,
      componentProps: { value: 123, otherValue: 234 }
    });
    return await modal.present();
  }
}

在带有 Input 的模态组件中,您可以使用这些参数:

import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.scss']
})
export class TestComponent implements OnInit {
  @Input("value") value;
  @Input() otherValue;
  constructor() { }

  ngOnInit() {
    //print 123
    console.log(this.value);
    //print 234
    console.log(this.otherValue);
  }
}
于 2018-08-23T17:03:48.990 回答
4

你也可以使用 Navparams 来获取 componentProps 的值。

import { CommentModalPage } from '../comment-modal/comment-modal.page';
import { ModalController, IonContent } from '@ionic/angular';


constructor(public modalCtrl : ModalController) {  }

  async commentModal() {
      const modal = await this.modalCtrl.create({
        component: CommentModalPage,

        componentProps: { value: 'data'}
      });
      return await modal.present();
   }

在您的commentModalPage 中,您只需导入navprams 并从中获取价值。

import { NavParams} from '@ionic/angular';

constructor(public navParams : NavParams) {  

              console.log(this.navParams.get('value'));

            }
于 2019-04-12T06:35:36.877 回答
-5

只需将以下内容添加到您的模态页面:

public test: string;

那么您可以使用以下方法对其进行测试:

console.log(this.test); // Output will be '123'

资源

于 2019-01-03T23:42:38.913 回答