0

我最终得到以下代码:

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

1 回答 1

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>
于 2015-04-28T15:21:32.523 回答