Angularjs Modal:从服务返回状态

时间:2018-07-25 13:29:06

标签: javascript angularjs

在此处使用angularjs:

我在以下论坛中进行了查询,但无法解决此问题:returning data from angularjs modal dialog service

我有以下模态html:

<div class="confirm-modal-header">
  <h4 class="modal-title">{{modalOptions.headerText}}</h4>
 </div>
<div class="modal-body">
<p>{{modalOptions.bodyText}}</p>
</div>
    <div class="modal-footer">
    <button type="button" class="btn"
        data-ng-click="modalOptions.close()">
    {{modalOptions.closeButtonText}}
    </button>
    <button type="button" class="btn btn-danger"
        data-ng-click="modalOptions.ok();">
    {{modalOptions.actionButtonText}}
    </button>
</div>

我也有服务:

CTApp.factory("dialogModalService", ["$q", "$timeout", "$state", "$modal", 
function ($q, $timeout, $state, $modal) {

    var service = {}  

    var modalDefaults = {
        backdrop: true,
        keyboard: true,
        modalFade: true,
        templateUrl: '/app/modal.html'
    };

    var modalOptions = {
        closeButtonText: 'Close',
        actionButtonText: 'OK',
        headerText: 'Proceed?',
        bodyText: 'Perform this action?'
    };

    service.showModal = function (customModalDefaults, customModalOptions) {
        if (!customModalDefaults) customModalDefaults = {};
        customModalDefaults.backdrop = 'static';
        return this.show(customModalDefaults, customModalOptions);
    };

    service.show = function (customModalDefaults, customModalOptions) {
        //Create temp objects to work with since we're in a singleton service
        var tempModalDefaults = {};
        var tempModalOptions = {};

        //Map angular-ui modal custom defaults to modal defaults defined in service
        angular.extend(tempModalDefaults, modalDefaults, customModalDefaults);

        //Map modal.html $scope custom properties to defaults defined in service
        angular.extend(tempModalOptions, modalOptions, customModalOptions);

        if (!tempModalDefaults.controller) {
            tempModalDefaults.controller = function ($scope, $modalInstance) {
                $scope.modalOptions = tempModalOptions;
                $scope.modalOptions.ok = function (result) {
                    $modalInstance.close(result);
                };
                $scope.modalOptions.close = function (result) {
                    $modalInstance.dismiss('cancel');
                };
            }
        }

        return $modal.open(tempModalDefaults).result;
    };

    return service;
}

]);

上面的模式从我的控制器中被调用为:

 dialogModalService.showModal({}, modalOptions).then(function (result) 
 {
    //check for result returned
 }

现在在我的控制器中,我想检查用户是否按下了取消按钮。我该如何检查?

我尝试将取消与

一起传递
modalOptions.close('cancel')

但它永远不会进入我的dialogModalService.showModal的控制器循环。

在我的浏览器中,我也试图在上面的调用中放置一个断点,但是再也没有成功。

我在这里做什么错了?

1 个答案:

答案 0 :(得分:1)

在您的Modal服务中,尝试更改:

$modalInstance.dismiss('cancel');

$modalInstance.close('cancel');