确认值未从甜蜜警报服务返回

时间:2016-03-28 07:40:37

标签: angularjs sweetalert

已经创建了甜蜜的警报作为单独的服务,我注入了我的服务

这是甜蜜警报服务

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

然后在控制器中注入甜蜜警报服务,但这里没有返回确认选择值。 isConfirm值未在控制器中到达

  (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 个答案:

答案 0 :(得分:4)

我认为您应该更改sweeet alert确认框的实施。甜蜜警报器的方法confirm,您需要传递callback执行到confirm方法并在那里执行。

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

<强>控制器

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