Eik*_*ies 7 javascript confirm directive onclick angularjs
我正在使用此脚本在实际触发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();
}
});
}
}
}
]);
Run Code Online (Sandbox Code Playgroud)
如 http://zachsnow.com/#!/blog/2013/confirming-ng-click/所示
通过以下方式使用:
<button ng-confirm-click="Are you sure?" ng-click="remove()">Remove</button>
Run Code Online (Sandbox Code Playgroud)
There are other similar scripts here on SO, but since i updated to Angular 1.2 RC3 they stopped working. The ng-click function is always fired BEFORE the actual link function is stepped into.
I also tried to increase priority and to listen to other events(touchstart, because the newest angular has this new ngtouch directives). But nothing works.
Eik*_*ies 14
啊,我自己解决了!
最近,有角度的团队提交了改变前/后链接优先级的提交:https: //github.com/angular/angular.js/commit/31f190d4d53921d32253ba80d9ebe57d6c1de82b
现在包含在Angular 1.2 RC3中!
因此,后链接功能现在具有相反的优先级.
所以有两种方法可以解决这个问题.无论是现在使用一个负的优先级
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();
}
});
}
}
}
]);
Run Code Online (Sandbox Code Playgroud)
或者将函数转换为预链接函数
Directives.directive('ngConfirmClick', [
function(){
return {
priority: 100,
restrict: 'A',
link: {
pre: function(scope, element, attrs){ //<---------
element.bind('click touchstart', function(e){
var message = attrs.ngConfirmClick;
if(message && !window.confirm(message)){
e.stopImmediatePropagation();
e.preventDefault();
}
});
}
}
}
}
]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3282 次 |
| 最近记录: |