AngularJS 中的确认指令无效

Confirm directive in AngularJS doesn't work

我最终得到了这段代码:

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;
                            }
                        });
                });
            }
        }
    };
}
]);

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>