2016-03-28 7 views
2

から戻っていない別々のサービスとイム注入として甘いアラートを作成していた私のサービスを印籠その確認値が甘いのアラートサービス

これは、コントローラに続いて甘い警告甘い、アラートサービス

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

     })(); 

答えて

3

に達していない私はあなたがsweeet alert確認ボックスの実装を変更すべきだと思います。方法の甘い警報の方法confirmは、callbackconfirmメソッドに実行し、そこに実行するために渡す必要があります。

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

は、私は、コールバックを通じてそれらの値を返すことができますか()? – user630209

+0

@ user630209コールバックに 'isConfrimed'フラグを渡すことでコールバックを実行することができます。 –

+0

yaは正常に動作しますが、なぜevent.preventDefault();期待どおりに動作していませんか? – user630209