confirm value is not returning from sweet alert service

China☆狼群 提交于 2019-12-19 04:32:11

问题


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();
                }
                };

        })();

回答1:


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();
    }
  });
};


来源:https://stackoverflow.com/questions/36258051/confirm-value-is-not-returning-from-sweet-alert-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!