Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modal confirmation as an Angular UI directive

I'm trying to make an Angular directive with angular-bootstrap to mimic the confirm() function.

Here is a plunk showing the visual result and behavior I want to achieve : http://embed.plnkr.co/27fBNbHmxx144ptrCuXV/preview

Now I would like to use a directive to call the modal window :

<div ng-controller="ModalDemoCtrl">
     <ul>
         <li ng-repeat="item in items">
              {{ item }} <a ng-really-message="Are you sure ?" ng-really-click="reallyDelete(item)">Delete</a>
         </li>
     </ul>
</div>

I made a working directive which uses the 'confirm()' function, but when I try to use the modal window instead of the confirm function, I get a "$digest already in progress" error.

The plunk : http://plnkr.co/edit/JSOInyZIvMtBZFaNvQRO?p=preview

var ModalDemoCtrl = function($scope, $modal) {

  $scope.items = ['item1', 'item2', 'item3'];

  $scope.reallyDelete = function(item) {
    $scope.items = window._.remove($scope.items, function(elem) {
      return elem != item;
    });
  };
};

angular.module('ngReallyClickModule', ['ui.bootstrap'])
  .directive('ngReallyClick', ['$modal',
    function($modal) {

      var ModalInstanceCtrl = function($scope, $modalInstance) {
        $scope.ok = function() {
          $modalInstance.close();
        };

        $scope.cancel = function() {
          $modalInstance.dismiss('cancel');
        };
      };

      return {
        restrict: 'A',
        link: function(scope, element, attrs) {
          element.bind('click', function() {
            var message = attrs.ngReallyMessage || "Are you sure ?";

            /*
            //This works
            if (message && confirm(message)) {
              scope.$apply(attrs.ngReallyClick);
            }
            //*/

            //*This doesn't work
            var modalHtml = '<div class="modal-body">' + message + '</div>';
            modalHtml += '<div class="modal-footer"><button class="btn btn-primary" ng-click="ok()">OK</button><button class="btn btn-warning" ng-click="cancel()">Cancel</button></div>';

            var modalInstance = $modal.open({
              template: modalHtml,
              controller: ModalInstanceCtrl
            });

            modalInstance.result.then(function() {
              scope.$apply(attrs.ngReallyClick); //raise an error : $digest already in progress
            }, function() {
              //Modal dismissed
            });
            //*/

          });

        }
      }
    }
  ]);

I

like image 698
mmai Avatar asked Mar 01 '14 10:03

mmai


1 Answers

Try function binding (&):

scope:{
    ngReallyClick:"&", //declare a function binding for directive
    item:"=" //the current item for the directive
},

Your html:

<a ng-really-message="Are you sure ?" ng-really-click="reallyDelete(item)" item="item">Delete</a>

Call your function when the user clicks ok:

modalInstance.result.then(function() {
     scope.ngReallyClick({item:scope.item}); //call the function though function binding
}, function() {
              //Modal dismissed
});

DEMO

like image 143
Khanh TO Avatar answered Oct 16 '22 03:10

Khanh TO