gpt4 book ai didi

forms - 我如何在 ionic 2 中将数据从带有表单的模态传递到主页?

转载 作者:行者123 更新时间:2023-12-01 04:36:38 25 4
gpt4 key购买 nike

您好,谁能帮忙。我是 Ionic 和 Angular 的新手。我正在尝试在 Ionic 2 中创建一个天气应用程序。我创建了一个带有调用 AddWeather() 函数的 Click 事件的主页。该函数打开一个带有附加表单的模式。我正在尝试将数据从模式上的表单传递回主页,并在屏幕上输出。我已经尝试过 ngModel 但似乎无法使其正常工作。

这是我的代码:(home.ts)

import { Component } from '@angular/core';
import { NavController, ModalController, NavParams } from 'ionic-angular';
import { AddPage } from '../add/add';

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

public weatherList = [];
constructor(public navCtrl: NavController, public modalCtrl: ModalController) {

}

addWeather() {
let addWeatherModal = this.modalCtrl.create(AddPage);


addWeatherModal.present();
}
}

(add.html)

<ion-header>
<ion-navbar color="weatherbru">
<ion-title>Modal</ion-title>
<ion-buttons end>
<button ion-button icon-only (click)="CloseModal()"><ion-icon name="close"></ion-icon></button>
</ion-buttons>
</ion-navbar>
</ion-header>

<ion-content padding>
<form>
<ion-item>
<ion-label stacked>City</ion-label>
<ion-input type="text" ></ion-input>
</ion-item>
<ion-item>
<ion-label stacked>Country</ion-label>
<ion-select >
<ion-option value="us">United States</ion-option>
<ion-option value="uk">United Kingdom</ion-option>
</ion-select>
</ion-item>
<button ion-button block (click)="CloseModal()">Add Weather</button>
</form>
<div padding>
</div>
</ion-content>

(add.ts)

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

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

constructor(public viewCtrl: ViewController, public navCtrl: NavController, public navParams: NavParams) {}


CloseModal() {

this.viewCtrl.dismiss();
}

}

最佳答案

在你的 html 中:

<form>
<ion-item>
<ion-label stacked>City</ion-label>
<ion-input [(ngModel)]="form.city" type="text" ></ion-input>
</ion-item>
<ion-item>
<ion-label stacked>Country</ion-label>
<ion-select [(ngModel)]="form.country">
<ion-option value="us">United States</ion-option>
<ion-option value="uk">United Kingdom</ion-option>
</ion-select>
</ion-item>
<button ion-button block (click)="CloseModal()">Add Weather</button>
</form>

在你的 add.ts 文件中:

export class AddPage {
form : any; //Declare the form object to be bound to your html
constructor(public viewCtrl: ViewController, public navCtrl: NavController, public navParams: NavParams) {}

CloseModal() {

this.viewCtrl.dismiss(this.form); //Send back the form object when closeModal is called
}
}

在你的 home.ts 中:

addWeather() {
let addWeatherModal = this.modalCtrl.create(AddPage);

addWeatherModal.present();
addWeatherModal.onDidDismiss(data=>{ //This is a listener which wil get the data passed from modal when the modal's view controller is dismissed
console.log("Data =>", data) //This will log the form entered by user in add modal.
})
}

关于forms - 我如何在 ionic 2 中将数据从带有表单的模态传递到主页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41701347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com