我最终得到了这段代码:
MetronicApp.directive('confirmClick', ['SweetAlert',
function(SweetAlert) {
return {
priority: 100,
restrict: 'A',
scope: {
confirmClick: '&'
},
link: {
pre: function(scope, element, attr) {
var msg = attr.ngConfirmClick || "Are you sure?";
var clickAction = attr.confirmedClick;
element.bind('click touchstart', function(event) {
SweetAlert.swal({
title: "Are you sure?",
text: "Your will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: true
},
function(isConfirm) {
if (isConfirm) {
scope.confirmClick();
}
else{
return false;
}
});
});
}
}
};
}
]);
答案 0 :(得分:1)
ng-click="delete(theme)"
将始终由您点击
要做的是将你的函数传递给你的指令:
MetronicApp.directive('ngConfirmClick', ['SweetAlert',
function(SweetAlert) {
return {
priority: 100,
restrict: 'A',
scope: {
ngConfirmClick : '&'
},
link: {
pre: function(scope, element, attr) {
var msg = attr.ngConfirmClick || "Are you sure?";
var clickAction = attr.confirmedClick;
element.bind('click touchstart', function(event) {
SweetAlert.swal({
title: "Are you sure?",
text: "Your will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm) {
if (isConfirm) {
ngConfirmClick();
SweetAlert.swal("Deleted!", "Your imaginary file has been deleted.", "success");
} else {
SweetAlert.swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});
});
}
}
};
}
并使用指令:
<a ng-confirm-click="delete(theme)" class="btn btn-danger btn-md ng-scope" >Delete</a>
答案 1 :(得分:-3)
我看到你错过了支架['SweetAlert'添加支架并再试一次 在功能结束时添加它。
MetronicApp.directive('ngConfirmClick', ['SweetAlert',
function(SweetAlert) {
return {
priority: 100,
restrict: 'A',
link: {
pre: function(scope, element, attr) {
var msg = attr.ngConfirmClick || "Are you sure?";
var clickAction = attr.confirmedClick;
element.bind('click touchstart', function(event) {
SweetAlert.swal({
title: "Are you sure?",
text: "Your will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm) {
if (isConfirm) {
SweetAlert.swal("Deleted!", "Your imaginary file has been deleted.", "success");
} else {
SweetAlert.swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});
});
}
}
};}])