javascript – 防止点击从触发确认对话框(停止在Angular 1.2 RC3中工作)

我正在使用此脚本在实际触发ng-click功能之前有一个确认对话框
Directives.directive('ngConfirmClick',[
  function(){
    return {
      priority: 100,restrict: 'A',link: function(scope,element,attrs){
        element.bind('click',function(e){
          var message = attrs.ngConfirmClick;
          if(message && !confirm(message)){
            e.stopImmediatePropagation();
            e.preventDefault();
          }
        });
      }
    }
  }
]);

如上所见
http://zachsnow.com/#!/blog/2013/confirming-ng-click/

通过以下方式使用:

<button ng-confirm-click="Are you sure?" ng-click="remove()">Remove</button>

SO上还有其他类似的脚本,但自从我更新到Angular 1.2 RC3后,它们就停止了工作.在实际链接功能进入之前,始终触发ng-click功能.

我还试图提高优先级并听取其他事件(touchstart,因为最新的角度有这个新的ngtouch指令).但没有任何作用.

解决方法

啊,我自己解决了!

最近,有角度的团队提交了改变前/后链接优先级的提交:
https://github.com/angular/angular.js/commit/31f190d4d53921d32253ba80d9ebe57d6c1de82b

现在包含在Angular 1.2 RC3中!

因此,后链接功能现在具有相反的优先级.

所以有两种方法可以解决这个问题.
现在使用否定优先级

Directives.directive('ngConfirmClick',[
  function(){
    return {
      priority: -100,//<---------
      restrict: 'A',function(e){
          var message = attrs.ngConfirmClick;
          if(message && !confirm(message)){
            e.stopImmediatePropagation();
            e.preventDefault();
          }
        });
      }
    }
  }
]);

或者将函数转换为预链接函数

Directives.directive('ngConfirmClick',link: {
          pre: function(scope,attrs){ //<---------
                element.bind('click touchstart',function(e){
                  var message = attrs.ngConfirmClick;
                  if(message && !window.confirm(message)){
                    e.stopImmediatePropagation();
                    e.preventDefault();
                  }
                });
              }
        }
    }
  }
]);

相关文章

什么是深拷贝与浅拷贝?深拷贝与浅拷贝是js中处理对象或数据...
前言 今天复习了一些前端算法题,写到一两道比较有意思的题:...
最近在看回JavaScript的面试题,this 指向问题是入坑前端必须...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面