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

标签 angularjs sweetalert

已经创建了甜蜜的警报作为单独的服务,我将其注入(inject)到我的服务中

这是贴心的警报服务

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

然后在 Controller 中注入(inject)甜蜜警报服务,但这里它不返回确认选择值。 Controller 中未达到 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();
                }
                };

        })();

最佳答案

我认为你应该更改sweeetalert确认框的实现。 sweetalert的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)
  });
};

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

关于angularjs - 确认值未从甜蜜警报服务返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36258051/

相关文章:

javascript - ng 流未定义

javascript - 我怎样才能取消确认 sweetalert

javascript - SweetAlert,确认后聚焦输入

javascript - Jquery 替代甜蜜警报

localization - 本地化 Flash 消息?

javascript - Laravel 中 Sweet Alert 的删除方法

javascript - AngularJS : directive ng-repeat doesn't work in link function

javascript - 使用 Angular.js 和 PHP 在选择下拉列表中遇到问题

javascript - 使用 NodeJS 将 JSON 插入 MySQL JSON 列格式

javascript - Protractor browser.wait 导致 Angular 超时