1

Using angularjs here:

I have looked at the following forum for my query but not able to resolve this :returning data from angularjs modal dialog service

I have the following modal 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>

I also have the service for this:

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;
}

]);

And the above modal is being called from my controller as:

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

Now in my controller I want to check whether the user pressed the cancel button or not. How can I check that?

I tried to pass the cancel along with

modalOptions.close('cancel')

But it never enters my controller loop of the dialogModalService.showModal.

In my browser also I tried to put a breakpoint inside the call above but again its never hit.

What am I doing wrong here?

aman
  • 4,198
  • 4
  • 24
  • 36

1 Answers1

1

In your Modal service try changing:

$modalInstance.dismiss('cancel');

to

$modalInstance.close('cancel');
kaka1234
  • 770
  • 3
  • 9
  • 30
  • 1
    That was simple. This works fine. For others the reason being: close(result) - a method that can be used to close a modal, passing a result dismiss(reason) - a method that can be used to dismiss a modal, passing a reason result - a promise that is resolved when a modal is closed and rejected when a modal is dismissed – aman Jul 26 '18 at 01:25