下面是我的指令cabinet和我的两个 Controller CabinetThumbnailsModalCtrl的代码。根据我的要求,我正在使用指令cabinetCabinetThumbnails渲染几个小部件,然后单击每个使用ModalCtrl的小部件打开一个弹出窗口。窗口小部件生成正常,弹出窗口也正常打开,但是(uibModalInstance.result.then(function (thumbnailData))无法正常工作。因此,它也不会打到服务cabinetService.getComments(thumbnailData)。这是怎么回事?无法弄清楚。

function () {
    'use strict';

    angular
        .module('myModule')
        .directive('cabinet', function () {
            return {
                restrict: 'E',
                replace: true,
                controller: CabinetThumbnails,
                controllerAs: 'ctrl',
                bindToController: true,
                link: link,
                templateUrl: 'app/cabinet/cabinet.directive.html',
                scope: {
                    thumbnail: '='
                }
            };
        });
    function link(scope, el, attrs) {
    }

    CabinetThumbnails.$inject = ['$scope','$uibModal','cabinetService'];

    function CabinetThumbnails($scope,$uibModal,cabinetService) {
        var vm = this;

        vm.showImage = showImage;

        activate();

        function activate() {
        }

        function showImage() {
            var uibModalInstance = $uibModal.open({
                animation: true,
                templateUrl: 'app/components/capture/cabinet.pop-up.html',
                controller: ModalCtrl,
                controllerAs: 'ctrl',
                size: 'lg',
                resolve: {
                    thumbnailData: function () {
                        return vm.thumbnail;
                    }
                }
            });

            uibModalInstance.result.then(function (thumbnailData) {
                spinner.spinnerShow();

                //call the service to get the comments
                cabinetService
                    .getComments(thumbnailData)
                    .then(function (data) {
                      $scope.comments = data;
                    })
                    .catch(function (err) {
                        growl.err('Unable to open the screen shot, Please try later !', {ttl: 20000});
                    })
                    .finally(spinner.spinnerHide);
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        }
    }

    ModalCtrl.$inject = ['$scope', '$uibModalInstance', 'thumbnailData', 'growl'];

    function ModalCtrl($scope, $uibModalInstance, thumbnailData, growl) {
        var ctrl = this;

        ctrl.thumbnailData = thumbnailData;
        ctrl.cancel = cancel;

        function cancel() {
            $uibModalInstance.dismiss();
        }
    }
}());

最佳答案

这里有几件事情要考虑:

  • 首先,您在定义模式时不需要使用controllerAs,在ui-bootstrap中,您应该只使用controller: myModalController as vm(或者在您的情况下为ctrl)。
  • 在您的指令中,您正在定义controllerAs: 'ctrl',,但稍后使用vm
  • 在模态 Controller 中,您仅使用$uibModalInstance.dismiss()方法,dismiss方法关闭模态并激活uibModalInstance.result Promise的Promise拒绝处理程序。
    您应该使用$uibModalInstance.close()激活解析处理程序。否则所有这些代码将无法运行。

  • 我会这样写的
       spinner.spinnerShow();
    
       $uibModal.open({
           animation: true,
           templateUrl: 'app/components/capture/cabinet.pop-up.html',
           controller: ModalCtrl as ctrl,
           size: 'lg',
           resolve: {
               thumbnailData: function () {
                   return ctrl.thumbnail;
               }
           }
       }).result
            .then(function (thumbnailData) {
                //call the service to get the comments
                return cabinetService.getComments(thumbnailData);
            }, function() {
                 $log.info('Modal dismissed at: ' + new Date());
            }).then(function (data) {
                // we get to this 'then' after cabinetService.getComments finished
                $scope.comments = data;
            }).catch(function (err) {
                $log.err('Unable to open the screen shot, Please try later !', {ttl: 20000});
            }).finally(spinner.spinnerHide);
        }
    

    并添加
    ctrl.ok = function() {
        $uibModalInstance.close(valueForResolveHandler);
    };
    

    到ModalCtrl

    07-24 19:19