Confirm dialog box in angularjs

Believe It or Not picture Believe It or Not · Feb 19, 2016 · Viewed 50.6k times · Source

How can I apply confirm dialog box in below button in angularjs ?

<button class="btn btn-sm btn-danger" ng-click="removeUser($index)">Delete</button>

Just like this.

<span><a class="button" onclick="return confirm('Are you sure to delete this record ?')" href="delete/{{ item.id }}">Delete</span>

Update

Currently I am doing it like this

    function removeUser(index) {
      var isConfirmed = confirm("Are you sure to delete this record ?");
      if(isConfirmed){
        vm.users.splice(index, 1);
      }else{
        return false;
      }
    };

Answer

Mohideen bin Mohammed picture Mohideen bin Mohammed · Feb 19, 2016

Here is the snippets,

how your HTML should be,

<button class="btn btn-sm btn-danger" ng-confirm-click="Are you sure to delete this record ?" confirmed-click="removeUser($index)">Delete</button>

Please Include this directive in your custom angularjs file,

app.directive('ngConfirmClick', [
    function(){
        return {
            link: function (scope, element, attr) {
                var msg = attr.ngConfirmClick || "Are you sure?";
                var clickAction = attr.confirmedClick;
                element.bind('click',function (event) {
                    if ( window.confirm(msg) ) {
                        scope.$eval(clickAction)
                    }
                });
            }
        };
}])

Your angular scope based on your delete function mentioned above,

$scope.removeUser = function(index) {
    vm.users.splice(index, 1);
}