AngularJS中的Confirm指令不起作用

时间:2015-04-28 15:11:01

标签: angularjs angular-directive sweetalert

我最终得到了这段代码:

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

2 个答案:

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