简体   繁体   English

模态确认作为Angular UI指令

[英]Modal confirmation as an Angular UI directive

I'm trying to make an Angular directive with angular-bootstrap to mimic the confirm() function. 我正在尝试使用angular-bootstrap创建一个Angular指令来模仿confirm()函数。

Here is a plunk showing the visual result and behavior I want to achieve : http://embed.plnkr.co/27fBNbHmxx144ptrCuXV/preview 这是一个显示我想要实现的视觉效果和行为的插图: 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. 我做了一个使用'confirm()'函数的工作指令,但是当我尝试使用模态窗口而不是confirm函数时,我得到一个“ $digest already in progress ”错误。

The plunk : http://plnkr.co/edit/JSOInyZIvMtBZFaNvQRO?p=preview 插件: 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 一世

Try function binding ( & ): 尝试功能绑定( & ):

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

Your html: 你的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 DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM