简体   繁体   English

如何在不单击“取消”按钮的情况下关闭Ionic2 Datetime弹出窗口

[英]How to close Ionic2 Datetime popup without clicking Cancel button

In an Ionic 2 project, I need make the user logout after a certain idle timeout. 在Ionic 2项目中,我需要在一定的空闲超时后使用户注销。 While doing so, I noticed that I cannot close the Datetime popup is before invoking the logout event and redirecting to the login page. 虽然这样做,我注意到,我不能关闭日期时间弹出窗口调用注销事件,并重定向到登录页面之前。 I need to close that popup before redirecting to the login page. 我需要重定向到登录页面之前关闭该弹出窗口。 Below is the code sample I an working on 下面是我正在处理的代码示例

<ion-datetime (ionCancel)="onCancelDateTime()" [(ngModel)]="dateTime">

</ion-datetime>

The (ionCancel) event is fired when close the popup. (ionCancel)事件关闭弹出窗口时触发。

Is there a way to invoke the firing of this event programmatically? 有没有办法以编程方式调用此事件的触发?

Or is there another way to close this Datetime popup? 还是有其他方法可以关闭此Datetime弹出窗口?

Currently there is no official documented Ionic 3 way to close the datetime picker programmatically. 目前,尚无官方记录的Ionic 3方法以编程方式关闭日期时间选择器。

However we can use Javascript 'dispatchEvent' method to trigger a click on the 'Cancel' button of Datetime picker. 但是,我们可以使用Javascript'dispatchEvent'方法来触发对Datetime选择器的'Cancel'按钮的单击。

Here is how to do it: 这是操作方法:

// Get the reference to the clear button of Datetime picker.
var pickerClearButton = document.getElementsByClassName("picker-button")[0];

// Create a click event to be triggered
var clickEvent = new MouseEvent("click", {
    "view": window,
    "bubbles": true,
    "cancelable": false
});

// Trigger the event
pickerClearButton.dispatchEvent(clickEvent);

I believe this will do the job!! 我相信这会做的!

Solved : In the html in the element add 解决:在html中的元素中添加

<ion-datetime #dateTime .....></ion-datetime>

In the controller : 在控制器中:

@ViewChild("dateTime") dateTime : DateTime;

// close function :=> 
// you can use this function in a timeout if you like to close it that way
public closeDateTimeModal():void {
  this.dateTime._picker.dismiss();
}

Tested and its working. 经过测试及其工作。 Cheers! 干杯!

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

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