Confirmation dialog on ng-click - AngularJS

后端 未结 17 1489
一向
一向 2020-11-30 00:03

I am trying to setup a confirmation dialog on an ng-click using a custom angularjs directive:

app.directive(\'ngConfirmClick\', [
    function()         


        
17条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 00:46

    You don't want to use terminal: false since that's what's blocking the processing of inside the button. Instead, in your link clear the attr.ngClick to prevent the default behavior.

    http://plnkr.co/edit/EySy8wpeQ02UHGPBAIvg?p=preview

    app.directive('ngConfirmClick', [
      function() {
        return {
          priority: 1,
          link: function(scope, element, attr) {
            var msg = attr.ngConfirmClick || "Are you sure?";
            var clickAction = attr.ngClick;
            attr.ngClick = "";
            element.bind('click', function(event) {
              if (window.confirm(msg)) {
                scope.$eval(clickAction)
              }
            });
          }
        };
      }
    ]);
    

提交回复
热议问题