在数组长度等于零后自动关闭uibModal

问题描述 投票:0回答:1

我们的团队正在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);
}

关于如何使模式自动关闭的任何建议?

angularjs modal-dialog angular-ui-bootstrap servicenow
1个回答
0
投票

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

$scope.$watch(function () {  
    return $scope.attachments;
}, function (value) {  
    if(value.length==0){
        if(c.modalInstance){
            c.modalInstance.close();
        }
    }
});  
© www.soinside.com 2019 - 2024. All rights reserved.