Confirmation dialog on ng-click - AngularJS

后端 未结 17 1374
一向
一向 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条回答
  • 2020-11-30 00:46

    You don't want to use terminal: false since that's what's blocking the processing of inside the button. Instead, in your link clear the attr.ngClick to prevent the default behavior.

    http://plnkr.co/edit/EySy8wpeQ02UHGPBAIvg?p=preview

    app.directive('ngConfirmClick', [
      function() {
        return {
          priority: 1,
          link: function(scope, element, attr) {
            var msg = attr.ngConfirmClick || "Are you sure?";
            var clickAction = attr.ngClick;
            attr.ngClick = "";
            element.bind('click', function(event) {
              if (window.confirm(msg)) {
                scope.$eval(clickAction)
              }
            });
          }
        };
      }
    ]);
    
    0 讨论(0)
  • 2020-11-30 00:47

    Confirmation dialog can implemented using AngularJS Material:

    $mdDialog opens a dialog over the app to inform users about critical information or require them to make decisions. There are two approaches for setup: a simple promise API and regular object syntax.

    Implementation example: Angular Material - Dialogs

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 00:48

    For me, https://www.w3schools.com/js/js_popup.asp, the default confirmation dialog box of the browser worked a great deal. just tried out this:

    $scope.delete = function() {
        if (confirm("sure to delete")) {
            // todo code for deletion
        }
    };
    

    Simple.. :)
    But I think you can't customize it. It will appear with "Cancel" or "Ok" button.

    EDIT:

    In case you are using ionic framework, you need to use the ionicPopup dialog as in:

    // A confirm dialog
    
    
    $scope.showConfirm = function() {
       var confirmPopup = $ionicPopup.confirm({
         title: 'Delete',
         template: 'Are you sure you want to delete this item?'
       });
    
       confirmPopup.then(function(res) {
         if(res) {
           // Code to be executed on pressing ok or positive response
           // Something like remove item from list
         } else {
           // Code to be executed on pressing cancel or negative response
         }
       });
     };
    

    For more details, refer: $ionicPopup

    0 讨论(0)
  • 2020-11-30 00:49

    HTML 5 Code Sample

    <button href="#" ng-click="shoutOut()" confirmation-needed="Do you really want to
    shout?">Click!</button>
    

    AngularJs Custom Directive code-sample

    var app = angular.module('mobileApp', ['ngGrid']);
    app.directive('confirmationNeeded', function () {
        return {
        link: function (scope, element, attr) {
          var msg = attr.confirmationNeeded || "Are you sure?";
          var clickAction = attr.ngClick;
          element.bind('click',function (e) {
            scope.$eval(clickAction) if window.confirm(msg)
            e.stopImmediatePropagation();
            e.preventDefault();
           });
         }
        };
    });
    
    0 讨论(0)
  • A clean directive approach.

    Update: Old Answer (2014)

    It basically intercepts the ng-click event, displays the message contained in the ng-confirm-click="message" directive and asks the user to confirm. If confirm is clicked the normal ng-click executes, if not the script terminates and ng-click is not run.

    <!-- index.html -->
    <button ng-click="publish()" ng-confirm-click="You are about to overwrite your PUBLISHED content!! Are you SURE you want to publish?">
      Publish
    </button>
    
    // /app/directives/ng-confirm-click.js
    Directives.directive('ngConfirmClick', [
      function(){
        return {
          priority: -1,
          restrict: 'A',
          link: function(scope, element, attrs){
            element.bind('click', function(e){
              var message = attrs.ngConfirmClick;
              // confirm() requires jQuery
              if(message && !confirm(message)){
                e.stopImmediatePropagation();
                e.preventDefault();
              }
            });
          }
        }
      }
    ]);
    

    Code credit to Zach Snow: http://zachsnow.com/#!/blog/2013/confirming-ng-click/

    Update: New Answer (2016)

    1) Changed prefix from 'ng' to 'mw' as the former ('ng') is reserved for native angular directives.

    2) Modified directive to pass a function and message instead of intercepting ng-click event.

    3) Added default "Are you sure?" message in the case that a custom message is not provided to mw-confirm-click-message="".

    <!-- index.html -->
    <button mw-confirm-click="publish()" mw-confirm-click-message="You are about to overwrite your PUBLISHED content!! Are you SURE you want to publish?">
      Publish
    </button>
    
    // /app/directives/mw-confirm-click.js
    "use strict";
    
    var module = angular.module( "myApp" );
    module.directive( "mwConfirmClick", [
      function( ) {
        return {
          priority: -1,
          restrict: 'A',
          scope: { confirmFunction: "&mwConfirmClick" },
          link: function( scope, element, attrs ){
            element.bind( 'click', function( e ){
              // message defaults to "Are you sure?"
              var message = attrs.mwConfirmClickMessage ? attrs.mwConfirmClickMessage : "Are you sure?";
              // confirm() requires jQuery
              if( confirm( message ) ) {
                scope.confirmFunction();
              }
            });
          }
        }
      }
    ]);
    
    0 讨论(0)
提交回复
热议问题