[ionic4] 模态对话框的深入理解以及Modal之间传值

本文围绕Ionic中模态对话框展开。首先介绍了模态对话框的使用场景,即用户操作对话框外应用程序前需先响应对话框。接着详细阐述建立模态对话框的过程,包括新建页面、创建组件、引入并声明组件、引入组件和控制器弹出对话框。最后提到模态对话框传值及使用时要多参考官网。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:

说实话,在搞ionic的时候,还真的应该简单的angular的帮助,要不然感觉理解期间的含义还真是有点难度的,不知道你们是不是,反正这篇博客做了一段ionic项目后,才写出来!

一.什么时候该用模态对话框

Modal(模态)对话框,是指用户想要对对话框以外的应用程序进行操作时,必须首先对该对话框进行响应.如单机确定或者取消按钮才能将对话框关闭!如下图就是一个模态对话框,点击取消或者确认并提醒按钮,就会关闭当前页面!回到你要操作的页面,它是从页面底部弹出的,如果是普通的页面跳转是左右转换的!
在这里插入图片描述

二.建立模态对话框的过程

1.新建立一个页面prientscreencheck, 此时在这个页面我们演示模态对话框
2.在本页面创建一个组件
ionic  g  component prientscreencheck/components/mistakeinfo

效果图:此时mistakeinfo是一个组件,我们的页面prientscreencheck要引用这个组件
在这里插入图片描述

3.在prientscreencheck.module.ts中引入mistakeinfo 组件,并且声明
    import { MistakeinfoComponent } from './components/mistakeinfo/mistakeinfo.component';
@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    RouterModule.forChild(routes),

  ],
  declarations: [PrientscreencheckPage,MistakeinfoComponent],
  entryComponents:[MistakeinfoComponent]   //模态对话框要用到
})
4.在prientscreencheck.page页面中引入刚才创建的Mistakeinfo组件,并且引入ModalController 弹出模态对话框, 代码如下
import { Component, OnInit, ViewChild } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { InterceptorService } from '../../../shared/providers/interceptor.service';

//mistakeinfo组件
import { MistakeinfoComponent } from './components/mistakeinfo/mistakeinfo.component';

@Component({
  selector: 'app-prientscreencheck',
  templateUrl: './prientscreencheck.page.html',
  styleUrls: ['./prientscreencheck.page.scss'],
})

export class PrientscreencheckPage implements OnInit {
  @ViewChild('slideMainContent') slideMainContent;
  constructor(
    public modalController: ModalController,
    public CommonService: InterceptorService
  ) { }

  public listStuInfo: any = [];
  public list: any = [];
  public listScreenPicture: any = [];

  ngOnInit() {
    for (let i = 0; i < 10; i++) {
      this.list.push(i);
    }
    this.getStudentInfo();
  }

//创建了模态对话框
  async showModel(item) {
    const modal = await this.modalController.create({
      component: MistakeinfoComponent,
      componentProps: {          //向模态窗体传值
        studentID:item.studentID,
        userName:item.userName,
        listScreenPicture:item.listPicURL 
       }   
    });
    await modal.present();
    //监听销毁的事件,此时data存的是模态窗口页面销毁时候的值
    const{data}=await modal.onDidDismiss();
    console.log(data);
  }

三.模态对话框的传值

import { Component, OnInit } from '@angular/core';
import { InterceptorService } from '../../../../../shared/providers/interceptor.service';  //请求

// 接受prientscreencheck页面传过来的值
import { NavParams } from '@ionic/angular';

@Component({
  selector: 'app-mistakeinfo',
  templateUrl: './mistakeinfo.component.html',
  styleUrls: ['./mistakeinfo.component.scss'],
})


export class MistakeinfoComponent implements OnInit {
  //不通过学生的id
  public studentID;

  //不通过学生的名字
  public userName;

  //操作人id
  public operator;

  //学生缴费截图
  public pictureUrl;

  //照片不通过的备注信息
  public remark;

  //不通过的时候返回给后端的信息
  MistakeInfo = {
    remark: '',
    operator: '',
    checkState: 0,
    studentID:''
  };


  //接收prientscreencheck页面传过来的参数
  constructor(public navParams: NavParams,
    public http:InterceptorService,) {
    this.studentID = this.navParams.data.studentID;
    this.userName = this.navParams.data.userName;
    this.operator = localStorage.getItem('userName'); //从缓存获取的操作人用户名
    this.pictureUrl = this.navParams.data.listScreenPicture;
  }

  ngOnInit() { }

  //关闭模态对话框
  doClose() {
    this.navParams.data.modal.dismiss({
      result: {
        'msg': '消失的时候返回的内容'
      }
    });
  }

  //更新学生截图错误的信息
  UpdateMistakeInfo() {
    this.MistakeInfo.remark = this.remark;
    console.log(this.MistakeInfo.remark);
    this.MistakeInfo.operator=localStorage.getItem('userName');
    this.MistakeInfo.checkState=3;
   this.MistakeInfo.studentID=this.studentID;

    const body=JSON.stringify(this.MistakeInfo);
    const api='NO1System/no.1/ScreenCheckController/UpdateRemarkById';
    this.http.post(api,body).subscribe();
    this.navParams.data.modal.dimiss();    //关闭模态对话框
  }
}

后记:

模态对话框很好用,还是多看官网。模态对话框是组件,引用模态对话框的时候需要在本页面的module.ts,page.ts下面引用相应的模态组件。

评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值