【问题标题】:Closing uibModal automatically after array length equals zero数组长度为零后自动关闭 uibModal
【发布时间】:2020-01-24 14:43:07
【问题描述】:

我们的团队正在 ServiceNow 中开发附件小部件。该小部件允许用户附加文档、查看它们并根据需要删除它们。为了查看附件,我们通过 uibModal 来利用模态窗口。如果用户删除了所有附件并且数组长度达到零,我们希望模式窗口自动关闭,但我们似乎无法正常工作。

<label>
  <sp-attachment-button></sp-attachment-button>
</label>

<span ng-if="attachments.length>0" class="badge" ng-click="c.openModal()">{{attachments.length}}</span>

<script type="text/ng-template" id="modalTemplate">
 <div class="panel panel-default">
   <div class="panel-heading flex">
     <h4 class="panel-title">Attachments</h4>
     <i type="button" class="fa fa-times" style="margin-left:auto;" ng-click="c.closeModal()"></i>
   </div>
   <div class="panel-body">
     <now-attachments-list template="sp_attachment_single_line"></now-attachments-list>
   </div>
   <!--<div class="panel-footer text-right">
     <button class="btn btn-primary" ng-click="c.closeModal()">${Close Modal}</button>
   </div>-->
 </div>
</script>

我们的控制器如下所示:

function ($uibModal, spModal, cabrillo, $scope, $http, spUtil, nowAttachmentHandler, $rootScope, $sanitize, $window, $sce) {
    var c = this;
    $scope.attachments=[];
    $scope.m = $scope.data.msgs;
    $scope.submitButtonMsg = $scope.m.submitMsg;

    if(c.options.case_sysid){
        $scope.data._attachmentGUID = c.options.case_sysid;
    }
    var ah = $scope.attachmentHandler = new nowAttachmentHandler(setAttachments, function() {});
    ah.setParams('sn_hr_core_case_workforce_admin', $scope.data._attachmentGUID, 1024 * 1024 * 24);
    function setAttachments(attachments, action) {
        $scope.attachments = attachments;
    }
    $scope.attachmentHandler.getAttachmentList();

    $scope.confirmDeleteAttachment = function(attachment) {
        if (cabrillo.isNative()) {
            if (confirm($scope.data.msgs.delete_attachment)) {
                $scope.attachmentHandler.deleteAttachment(attachment);
            }           
        } else {
            spModal.confirm($scope.data.msgs.delete_attachment).then(function() {
                $scope.attachmentHandler.deleteAttachment(attachment);
                if($scope.attachments.length==0){
                    c.modalInstance.close();
                }
            });
        }
    }

    c.openModal = function() {
        c.modalInstance = $uibModal.open({
            templateUrl: 'modalTemplate',
            scope: $scope
        });
    }

    c.closeModal = function() {
        c.modalInstance.close();
    }

    console.log('custom-attachments');
    console.log($scope);
}

关于如何让模态自动关闭有什么建议吗?

【问题讨论】:

  • 您是否尝试使用 $uibModalInstance.close() 关闭它?

标签: angularjs modal-dialog angular-ui-bootstrap servicenow


【解决方案1】:

我们想通了。我们必须观察数组,然后在数组达到零后关闭模态:

$scope.$watch(function () {  
    return $scope.attachments;
}, function (value) {  
    if(value.length==0){
        if(c.modalInstance){
            c.modalInstance.close();
        }
    }
});  

【讨论】:

    猜你喜欢
    • 2010-09-22
    • 2012-09-06
    • 2021-02-17
    • 2017-02-26
    • 1970-01-01
    • 2018-10-10
    • 2017-01-25
    • 1970-01-01
    相关资源
    最近更新 更多