user630209
user630209

Reputation: 1207

confirm value is not returning from sweet alert service

Had created sweet alert as seperate service and Im injecting that inro my service

This is the sweet alert service

(function(){
    'use strict';
    angular.module('app.services')
        .factory('SweetAlert', SweetAlertService);

    SweetAlertService.$inject = [];
    function SweetAlertService( ) {

        var swal = window.swal;


        //public methods
        var self = {

            swal: function ( arg1, arg2, arg3) {
                    if( typeof(arg2) === 'function' ) {
                        swal( arg1, function(isConfirm){
                                arg2(isConfirm);
                        }, arg3 );
                    } else {
                        swal( arg1, arg2, arg3 );
                    }
            },
            success: function(title, message) {
                swal( title, message, 'success' );
            },
            confirm: function(title, message){
                 swal({
                        title: "Are you sure?",
                        text: "You will not be able to recover this imaginary file!",
                        type: "warning",
                        showCancelButton: true,
                        confirmButtonColor: '#DD6B55',
                        confirmButtonText: 'Ok',
                        cancelButtonText: "Cancel",
                        closeOnConfirm: true,
                        closeOnCancel: true
                     },
                     function(isConfirm){
                          return isConfirm;                         
                     });
            }

        };

        return self;
     }
})();

Then in the controller the sweet alert service is injected, but here its not returning the confirm select value. isConfirm value is not reaching in the controller

  (function() {
      'use strict';
      angular.module('app.controllers').controller("MasterController",
        MasterController);

      MasterController.$inject = ['$scope', '$window', '$http', 'SweetAlert'];


      function MasterController($scope, $window, $http, SweetAlert) {

            $scope.updateRow = function(row,event) {
                vm.clear();
                var updateRow = SweetAlert.confirm('Are you sure?');

                if (updateRow) {
                    vm.save(row.entity);
                }else{
                     event.preventDefault();
                }
                };

        })();

Upvotes: 2

Views: 4028

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136184

I think you should change implementation of sweeet alert confirm box. The way confirm method of sweet alert implement, you need to pass pass a callback to execute to confirm method and execute over there.

confirm: function(title, message, callback) {
  swal({
    title: "Are you sure?",
    text: "You will not be able to recover this imaginary file!",
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: '#DD6B55',
    confirmButtonText: 'Ok',
    cancelButtonText: "Cancel",
    closeOnConfirm: true,
    closeOnCancel: true
  },
  function(isConfirm) {
      callback(isConfirm)
  });
};

Controller

$scope.updateRow = function(row, event) {
  vm.clear();
  SweetAlert.confirm('Are you sure?', null, function(isConfirmed) {
    if (isConfirmed) {
      vm.save(row.entity);
    } else {
      event.preventDefault();
    }
  });
};

Upvotes: 4

Related Questions