处理我的角度Ng-Dialog模式中的按钮单击事件

时间:2016-02-23 05:16:31

标签: angularjs angularjs-directive angularjs-scope angularjs-ng-model

我有一个Angularjs应用程序。我点击提交按钮时有一个表单我正在打开一个ngModal弹出窗口。我不知道如何处理模态窗口中存在的按钮单击事件。我已附上以下代码供参考:

在我的控制器内:

 $scope.ModelValue="Username:"
 $scope.displayModal = function () {
        ngDialog.open({template: 'app/modal/modal.html', scope: $scope});
    }

在我的Modal.html

<div class="modal-dialog">
  <div class="modal-content">
    <div class="modal-header primary">
      <h4 class="modal-title" id="myModalLabel">Confirmation</h4>
    </div>
    <div id= "modal-section" class="modal-body">
      {{ModelValue}}
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-warning" data-dismiss="modal">Close</button>
      <button type="button" class="btn btn-primary" data-dismiss="modal">Confirm</button>
    </div>
  </div>
</div>

在我的指令中:

'use strict';
angular.module('myApp')
  .directive('modal', function ($parse) {
    return {
      template: '<div class="modal fade">' +
      '<div class="modal-dialog">' +
        '<div class="modal-content">' +
          '<div class="modal-header">' +
            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' +
            '<h4 class="modal-title">{{ title }}</h4>' +
          '</div>' +
          '<div class="modal-body" ng-transclude></div>' +
        '</div>' +
      '</div>' +
    '</div>',
    restrict: 'E',
    transclude: true,
    replace:true,
    scope:true,
    link: function postLink(scope, element, attrs) {
      scope.title = attrs.title;
      scope.$watch(attrs.visible, function(value){
        if(value == true)
          element.modal('show');
        else
          element.modal('hide');
      });
      $(element).on('shown.bs.modal', function(){
        scope.$apply(function(){
          scope.$parent[attrs.visible] = true;
        });
      });

      $(element).on('hidden.bs.modal', function(){
        scope.$apply(function(){
          scope.$parent[attrs.visible] = false;
        });
      });
    }
  };
});

请让我知道如何处理确认和关闭按钮事件点击。在确认按钮上单击我需要处理我打开对话框的父控制器内的事件。

1 个答案:

答案 0 :(得分:0)

按下定义的呼叫按钮

<div class="ngdialog-buttons col-sm-8 col-sm-offset-2">

 <button type="button" id="btnClose" class="ngdialog-button ngdialog-button-secondary" ng-click=closeThisDialog("Cancel")>Cancel</button> 

 <button type="button" class="ngdialog-button ngdialog-button-primary" ng-click=updateForm()>Save Changes</button>

 </div>