使用JS在Angular中为删除按钮创建确认警报

时间:2013-03-14 21:55:07

标签: javascript angularjs confirmation

我有一个带删除按钮的表单,我想创建一个单击删除按钮时弹出的确认框。删除按钮当前有效。我在javascript中尝试过几个没有运气的东西。我正在使用Angular。

这是最佳方法吗?

此外,有没有人知道任何这方面的例子,我没有找到任何工作。

$(document).ready(function(){
  $("form").validate();
  $(".radius small success button").ConfirmDialog('Are you sure?');
});

5 个答案:

答案 0 :(得分:41)

看起来像一个AngularJS指令对于解决这个问题有点过分了。使用直接javascript似乎更容易,除非您需要一些自定义功能,以确认(" confirm()"功能

if (confirm('Are you sure you want to delete this?')) {
     // TODO:  Do something here if the answer is "Ok".
}

希望这会有所帮助,欢呼

  

UPDATE :实际上,使用Angular,最好使用$ window.confirm,因为这样可以让你用Karma / Jasmine进行测试。

答案 1 :(得分:29)

Here这是另一种方法。 基本上它是一个指令,它获取你想要显示的警告字符串,以及用户接受时调用的函数。用法示例:

<button type="button" ng-really-message="Are you sure?"
ng-really-click="delete()">Delete</button>

答案 2 :(得分:9)

这就是我们处理“确认对话框”(使用引导程序)的方式

标记

<div class="alert alert-block alert-error notification fade in" data-ng-show="displayLocationDeletePopup">
    <h6>Are you sure you want to delete this location?</h6>
    <div class="form-controls-alert">
        <a href="" class="btn" data-ng-click="showDeleteLocationPopup(false)">No</a>
        <a href="" class="btn btn-danger" data-ng-click="deleteVendorLocation(locationId)">Yes</a>
    </div>
</div><!-- end alert -->    

在控制器负载上将模型设置为false,默认情况下隐藏ng-show

$scope.displayLocationDeletePopup = false;

点击show popup的事件,在

中调用一个函数/传递模型
<i class="icon-remove" data-ng-click="showDeleteLocationPopup(true, location)"></i>

在控制器中:

$scope.showDeleteLocationPopup = function(options, id) {
    if (options === true) {
        $scope.displayLocationDeletePopup = true;
    } else {
        $scope.displayLocationDeletePopup = false;
    }
    $scope.locationId = id;
};

根据上面html中的锚点,可以关闭弹出窗口或运行函数

$scope.deleteVendorLocation = function (storeLocation) {
   // Code to run on confirmation            
};

答案 3 :(得分:-1)

var r = confirm("Are you sure you want to Permanently delete this order?");
if (r == true) {
    (OK button click) Write the function here.....
} else {
    (Cancle button click) Write the function here.....
}

答案 4 :(得分:-1)

将删除选项放在每条记录的右侧,单击删除选项时,记录应从详细信息和JSON数组中删除。