Confirmation dialog on ng-click - AngularJS

后端 未结 17 1503
一向
一向 2020-11-30 00:03

I am trying to setup a confirmation dialog on an ng-click using a custom angularjs directive:

app.directive(\'ngConfirmClick\', [
    function()         


        
17条回答
  •  Happy的楠姐
    2020-11-30 00:47

    Here is a clean and simple solution using angular promises $q, $window and native .confirm() modal:

    angular.module('myApp',[])
      .controller('classicController', ( $q, $window ) => {
        this.deleteStuff = ( id ) => {
          $q.when($window.confirm('Are you sure ?'))
            .then(( confirm ) => {
              if ( confirm ) {
                // delete stuff
              }
            });
        };
      });
    

    Here I'm using controllerAs syntax and ES6 arrow functions but it's also working in plain ol' ES5.

提交回复
热议问题