简体   繁体   English

如何从模态调用回调函数

[英]How to call a callback function from a modal

On load for my home page I ask my user to set a default city.在加载我的主页时,我要求我的用户设置默认城市。 This is one of two places in the app where the default city can be changed therefore I've created a service for it.这是应用程序中可以更改默认城市的两个地方之一,因此我为它创建了一个服务。

In the constructor of the home page I call a function on the service that opens a modal where user can select from list of cities.在主页的构造函数中,我调用服务上的一个函数,该函数打开一个模式,用户可以在其中从城市列表中进行选择。 I do not open the modal directly via home.ts , it's through a function on the service.我不直接通过home.ts打开模态,而是通过服务上的功能。 When that modal closes after catching the city selection , I need a way to change the defaultCity property on the home.ts page.当该模式在捕获城市选择后关闭时,我需要一种方法来更改home.ts页面上的defaultCity属性。

The documentation here shows how to pass a callback to the create() method on the modal controller, not necessarily how to pass a call back to a function which calls another function which creates the modal.此处的文档显示了如何将回调传递给模态控制器上的create()方法,不一定是如何将回调传递回调用创建模态的另一个函数的函数。 This question deals specifically with Ionic 2 modals.这个问题专门针对 Ionic 2 模态。 https://ionicframework.com/docs/api/components/modal/ModalController/ https://ionicframework.com/docs/api/components/modal/ModalController/

Home.ts主页.ts

    import { Component } from '@angular/core';
    import { LoadingController, ModalController, NavController, PopoverController } from 'ionic-angular';
    import { changeDefaultCity } from '../../services/changeDefaultCity.service';
    
    @Component({
        selector: 'page-home',
        templateUrl: 'home.html',
        providers: [changeDefaultCity] 
    })
    
    export class Home {
    
        defaultCity: string; 
        
            constructor (   public navCtrl: NavController,
                                    public modalCtrl: ModalController,
                                    public loading: LoadingController,
                                    public chngDfCity: changeDefaultCity) {
    
        
    /*if default city isn't selected yet then show city select options on modal*/
    
            if(!this.selectedCity){
    
                    let setDefaultCityFunction = function(dfCity){  
                                                           //CALLBACK I WANT TO USE
    
                                   this.defaultCity = dfCity; 
    
                              };
    
                  this.chngDfCity.presentModal(); //CALL A FUNCTION ON SERVICE WHICH OPENS MODAL
    
            }
    
    
        } // end of constructor
                
    }//END CLASS

myService.ts我的服务.ts

import { Injectable } from '@angular/core';
import { ModalController } from 'ionic-angular';
import { SetDefaultCity } from '../pages/set-default-city/set-default-city';


@Injectable()
export class changeDefaultCity {


defaultCity: string; 

    constructor(public modalCtrl: ModalController) {

}//end of constructor


/*Open modal and show the list of city options*/ 

    presentModal() {
    let modal = this.modalCtrl.create(SetDefaultCity);
    modal.present();
  }

  /*on city select, set the selection to defaultCity property*/  

    getDefaultCity(dfCity){

                this.defaultCity = dfCity; 
            
        }

  }//end of class 

setDefaultCity.html //PAGE PASSED TO MODAL setDefaultCity.html //PAGE PASSED TO MODAL

<ion-header>
   //header stuff
</ion-header>


<ion-content padding>


<ion-list [(ngModel)]="defaultCity" (ionChange)="setDefaultCity(defaultCity)" radio-group>
  <ion-list-header>
    
  </ion-list-header>

  <ion-item>
    <ion-label>New York</ion-label>
    <ion-radio value="New York"></ion-radio>
  </ion-item>
  <ion-item>
    <ion-label>Los Angeles</ion-label>
    <ion-radio value="Los Angeles"></ion-radio>
  </ion-item>
  <ion-item>
    <ion-label>San Francisco</ion-label>
    <ion-radio value="San Francisco"></ion-radio>
  </ion-item>
  <ion-item>
    <ion-label>Philadelphia</ion-label>
    <ion-radio value="philadelphia"></ion-radio>
  </ion-item>
 
</ion-list>

</ion-content>

setDefaultCity.ts设置默认城市.ts

/* TS FOR THE PAGE PASSED TO THE MODAL. I USE THIS TS TO PASS THE CITY SELECTION TO THE SERVICE WHICH SHOULD PASS IT BACK TO HOME.TS*/

import { Component } from '@angular/core';
import { NavController, ViewController } from 'ionic-angular';
import { changeDefaultCity } from '../../services/changeDefaultCity.service';


@Component({
  selector: 'page-set-default-city',
  templateUrl: 'set-default-city.html',
  providers: [changeDefaultCity] 
})
export class SetDefaultCity {

defaultCity: string; 

  constructor(public navCtrl: NavController, public chngDfCity: changeDefaultCity, public viewCtrl: ViewController) {  

 }//END OF CONSTRUCTOR

  ionViewDidLoad() {
    console.log('Hello SetDefaultCityPage Page');

}

   //CALL THIS FUNCTION ON CHANGE WHICH THEN PASSES THE SELECTION TO THE SERVICE.  

  setDefaultCity(defaultCity){

      this.defaultCity = defaultCity; 

      this.chngDfCity.getDefaultCity(this.defaultCity); //SERVICE

    
          this.viewCtrl.dismiss();       

  
  }//end setDefaultCity

}//end class

You'll need to return the city to a function in onDidDismiss() But first take out that service, there's no use to it and you just need to call a modal to get a city.您需要将城市返回到onDidDismiss()的函数,但首先取出该服务,它没有用,您只需要调用一个模态来获取城市。 It could be useful if you hade too many cities and want to call a service from a modal to get all the cities.如果您拥有太多城市并希望从模式调用服务以获取所有城市,这可能会很有用。

So in yout home.ts call the modal所以在你的 home.ts 中调用模态

presentModal() {
  let modal = this.modalCtrl.create(SetDefaultCity);
  modal.onDidDismiss(data =>{
    if(data != null) { // check if null in case user quits the page and dont select a city
      return data;
    } // use and else if you need to treat if the user haven't choose a city
  })
  modal.present();
}

In your modal you can dismiss passing parameters back, with navController you can't do this, so if someday you need to get call a page to do something and return a data, just use modal instead of the normal navCtrl.push() .在您的模态中,您可以关闭传递参数,使用navController您不能这样做,所以如果有一天您需要调用页面来执行某些操作并返回数据,只需使用 modal 而不是普通的navCtrl.push()

I don't know if you're using and我不知道你是否在使用和

setDefaultCity(){
  this.viewCtrl.dismiss(this.defaultCity);       
}

ionViewWillUnload(){ // or ionViewWillLeave
  this.viewCtrl.dismiss(null); // this way you can treat if a user haven't selected a city and has left the page
}

Hope this helps :D希望这会有所帮助:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM