2016-03-28 41 views
2

回國創造了甜蜜的警覺單獨的服務和Im注入該INRO我的服務確認值不甜提醒服務

這是甜蜜的提醒服務

(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,你需要通過一個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(); 
    } 
    }); 
}; 
+0

可以通過我的回調返回這些值()? – user630209

+0

@ user630209你可以通過向你的回調傳遞'isConfrimed'標誌來執行回調,這就是你需要的嗎? –

+0

亞工作正常,但爲什麼event.preventDefault();沒有按預期工作? – user630209