首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按ESC键时检测$mdDialog关闭

按ESC键时检测$mdDialog关闭
EN

Stack Overflow用户
提问于 2016-09-07 01:39:20
回答 1查看 1.5K关注 0票数 5

我有一个带有输入字段的$mdDialog。在关闭$mdDialog之前,会保存输入字段的内容。因此,当用户按下“关闭”按钮时,会调用一个函数来对数据执行某些操作并保存它们。但是,我在ESC上检测不到$mdDialog的关闭。是否可以在$mdDialog控制器中使用ESC检测关闭事件?

以下是示例代码。Codepen link

代码语言:javascript
复制
angular.module('MyApp', ['ngMaterial'])

.controller('AppCtrl', function($scope, $mdDialog, $rootScope) {
  //$rootScope is used only of this demo
  $rootScope.draft = '  ';

  $scope.showDialog = function(ev) {
    var msgDialog = $mdDialog.show({
      controller: 'DemoDialogCtrl',
      template: "<md-input-container><label>Text</label><input type='text' ng-model='myText' placeholder='Write text here.'></md-input-container><md-button ng-click='close()'>Close</md-button>",
    })

  };


});

(function() {
  angular
    .module('MyApp')
    .controller('DemoDialogCtrl', DemoDialogCtrl);

  DemoDialogCtrl.$inject = ['$scope', '$rootScope', '$mdDialog'];

  function DemoDialogCtrl($scope, $rootScope, $mdDialog) {


    $scope.close = function() {
      //$rootScope is used only of this demo
      // In real code, there are some other operations 
      $rootScope.draft = $scope.myText;

      $mdDialog.hide();
    }

  }
})();
代码语言:javascript
复制
<div ng-controller="AppCtrl" class="md-padding dialogdemoBasicUsage" id="popupContainer" ng-cloak="" ng-app="MyApp">
  <div class="dialog-demo-content" layout="row" layout-wrap="" layout-margin="" layout-align="center">
    <md-button class="md-primary md-raised" ng-click="showDialog($event)">
      Open Dialog
    </md-button>
    <div id="status">
      <p>(Text written in $mdDialog text field must appear here when user closes the $mddialog. User can press close button or press ESC button.
      </p>
      <b layout="row" layout-align="center center" class="md-padding">
                 Draft: {{draft}}
            </b>
    </div>
  </div>
</div>

EN

回答 1

Stack Overflow用户

发布于 2016-09-07 21:31:41

您应该使用promise api。

代码语言:javascript
复制
$mdDialog.show().finally(
   function onModalClose(){

   }
);

但是带有外部代码的接收器应该与其他机制一起使用,例如通过提供作用域。

代码语言:javascript
复制
var modalScope = $rootScope.$new(true);
$mdDialog.show({scope: modalScope}).finally(function(){
    $rootScope.draft = modalScope.myText;
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39354886

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档