mdDialog:捕获 onClose 事件 [英] mdDialog: catch the onClose event

查看:38
本文介绍了mdDialog:捕获 onClose 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个有角度的邮箱.当发送消息的弹出窗口关闭时,我需要保存草稿消息.

I'm creating a mailbox in angular. And I would need to save the draft message when the popup to send a message closes.

我知道有一些替代方案:

I know there are some alternatives:

scope.$on("$destroy", function () { saveMessage() });

和:

$mdDialog.show(...).finaly(function(){ saveMessage() });

但两者都不够:

  • 当对话框已经关闭时调用第一个.这是由于要求不可接受(有一个 iFrame 需要打开)
  • 第二个不在 mdDialog 的控制器范围内,将责任交给弹出窗口的调用者,而它应该在弹出窗口本身中.

所以我正在寻找在弹出窗口实际关闭之前调用函数的方法.类似 scope.$on("$mdDialogBeforeClose", function () { saveMessage() });

So I'm looking for way to call a function BEFORE the pop-up actually closes. Something like scope.$on("$mdDialogBeforeClose", function () { saveMessage() });

另一种选择是挂钩每个关闭事件.看起来很丑,但可能是解决方案.在这种情况下,我需要听退出按钮并在弹出窗口外单击(尽管我可能会禁用该功能)...

Another option would be to hook every close event. Seems ugly, but might be the solution. In that case I would need to listen to the escape-button and clicking outside the pop-up (Altough I might disable that function)...

有更好的想法吗?

谢谢!

编辑:

另外一个问题:如何捕捉escape-keypress事件?我试过 <md-dialog aria-label="List dialog" ng-keypress="keyPress($event)"> 但它甚至没有触发...

An addition question: How to catch the escape-keypress event? I tried <md-dialog aria-label="List dialog" ng-keypress="keyPress($event)"> but it's not even triggered...

推荐答案

也许可以使用 onRemoving 回调 -

Maybe use the onRemoving callback - CodePen

标记

<md-button class="md-primary md-raised" ng-click="vm.open($event)">自定义对话框</md-button><script type="text/ng-template" id="test.html"><md-dialog aria-label="测试">你好!</md-dialog>

Markup

JS

angular.module('app',['ngMaterial', 'ngMessages', 'material.svgAssetsCache'])

.controller('MyController', function($scope, $mdDialog) {
  this.open = function(ev) {
    $mdDialog.show(
      {
        templateUrl: "test.html",
        clickOutsideToClose: true,
        onRemoving: function (event, removePromise) {
          console.log(123);
        }
    });
  };
})

这篇关于mdDialog:捕获 onClose 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆