AngularJS中删除按钮确认指令

时间:2014-08-25 07:37:51

标签: javascript html angularjs

我正在使用此指令对删除按钮进行确认。但要么我点击取消,要么删除该应用程序。

<small class="btn" ng-click="delete_app(app.app_id)" ng-show="app.app_id" ng-confirm-click="Are you sure you want to delete this app? You will loose all data and metrics associated with this app."> Delete </small>

.directive('ngConfirmClick', [
    function(){
        return {
            link: function (scope, element, attr) {
                var msg = attr.ngConfirmClick || "Are you sure you want to delete this app? You will loose all data and metrics associated with this app.";
                var clickAction = attr.confirmedClick;
                element.bind('click',function (event) {
                    if ( window.confirm(msg) ) {
                        scope.$eval(clickAction)
                    }
                });
            }
        };
}])

1 个答案:

答案 0 :(得分:2)

您的指令代码似乎没有错 你可以写下面的html。
当然,您的控制器需要具有名为&#39; test&#39;的方法。被称为。

<button ng-confirm-click confirmed-click="test()">Test</button>

如果您点击“取消”&#39;确认对话框上的按钮,将无法正确调用该方法。

相关问题