1. 程式人生 > >angular全局確認框confirm

angular全局確認框confirm

template images default ons deferred post run section put

angular.module(‘custom-template‘, []).run(["$templateCache", function ($templateCache) {
  $templateCache.put("template/modal/confirmModelTemplate.html",
  ‘<div id="youModel" class="m-c">\n‘ +
  ‘ <div class="modal-header">\n‘ +
  ‘ <h4 class="modal-title">{{modalTitle}}</h4>\n‘ +
  ‘ </div>\n‘ +
  ‘ <div class="modal-body">{{modalContent}}</div>\n‘ +
  ‘ <div class="modal-footer" style="text-align: center;">\n‘ +
  ‘ <button type="button" class="btn btn-primary" ng-click="ok()">確定</button>\n‘ +
  ‘ <button type="button" class="btn btn-default" ng-click="cancel()">取消</button>\n‘ +
  ‘ </div>\n‘ +
  ‘</div>\n‘ +
  "");
}]);

然後再模塊中註入‘‘custom-template’;

打開該模塊的方法:

app.factory(‘Common‘, [‘$http‘, ‘$q‘, ‘$cookieStore‘, ‘$location‘, ‘$modal‘,
  function ($http, $q, $cookieStore, $location, $modal) {
    return {
      openConfirmWindow: function (modalTitle, modalContent, modalInstance) {
      var deferred = $q.defer();
      var confirmModal = $modal.open({
      backdrop: ‘static‘,
      templateUrl: ‘template/modal/confirmModelTemplate.html‘, // 指向確認框模板
      controller: ‘ConfirmCtrl‘,// 初始化模態控制器
      windowClass: "confirmModal",// 自定義modal上級div的class
      size: ‘sm‘, //大小配置
      resolve: {
        data: function () {
        return { modalTitle: modalTitle, modalContent: modalContent };//surgeonSug: $scope.surgeonSug,
      }
    }
  });
  // 處理modal關閉後返回的數據
  confirmModal.result.then(function () {
    if (!!modalInstance) {
      modalInstance.dismiss(‘cancel‘);
    }
    deferred.resolve();
      }, function () {
    });
    return deferred.promise;
    }
  }
}]);

關閉該模態框同時帶參過來:

app.controller(‘ConfirmCtrl‘, [‘$scope‘, ‘$modalInstance‘, ‘data‘,
  function ($scope, $modalInstance, data) {
  $scope.modalTitle = data.modalTitle;
  $scope.modalContent = data.modalContent;
  $scope.ok = function () {
  $modalInstance.close(true);
  };
  //關閉郵件框
  $scope.cancel = function () {
    $modalInstance.dismiss(‘cancel‘);
  };
}]);

在其他控制器調用該確認框:(首先註入Common)

Common.openConfirmWindow(‘刪除提示!‘, ‘確定刪除嗎?‘).then(function () {
  $http({
    method: ‘POST‘,
    url: ‘/contact/delete?id=‘ + deleteData.id,
    headers: { ‘Content-Type‘: undefined },
    transformRequest: angular.identity
  }).then(function (response) {
    $scope.toaster.text = response.data.message;
    if (response.data.success == false) {
      toaster.pop(‘error‘, ‘Error‘, $scope.toaster.text);
      return;
    }
    toaster.pop(‘success‘, ‘Success‘, $scope.toaster.text);
    $scope.ContactData();
  }, function (response) {
    $scope.toaster.text = ‘Network error. Please try again later!‘;
    toaster.pop(‘error‘, ‘Error‘, $scope.toaster.text);
  });
});

完成確認框之後會發現確認框在頂部,然後修改全局的css的‘.model‘樣式,所有模態框都垂直居中了,如下:

.modal {
  position: fixed;
  top: 0;
  /* right: 0;
  bottom: 0;
  left: 0; */
  z-index: 1040;
  display: none;
  overflow: hidden;
  -webkit-overflow-scrolling: touch;
  outline: 0;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
}

效果圖:

技術分享

angular全局確認框confirm