2

我在使用 AngularJS和为 AngularJSshowLoaderOnConfirm工作时遇到问题。sweetalert2ngSweetAlert

以下代码将正常执行且没有错误,但是,我没有等待动画。一旦请求被返回,警报将消失,然后再次弹出。

       $scope.passwordReset = function() {

            swal({
                title: 'Forgot Password?', 
                text: 'Enter your email address and your password will be reset and emailed to you.', 
                input: 'email', 
                showCancelButton: true, 
                confirmButtonText: 'Send', 
                confirmButtonColor: "#DD6B55", 
                inputPlaceholder: 'Email address', 
                showLoaderOnConfirm: true
            }).then(function( email ) {

                if( email ) {

                    AccountFactory.resetAccount( email )
                        .then(function( data ) {

                            swal({
                                title: 'Success!', 
                                text: 'A verification email has been sent to ' + email, 
                                type: 'success', 
                                confirmButtonText: 'Close', 
                                allowEscapeKey: false
                            });

                        }, function( error ) {

                            swal({
                                title: 'Email not found', 
                                text: 'Sorry, but we could not find an account matching that email address.', 
                                type: 'error', 
                                confirmButtonText: 'Close', 
                                allowEscapeKey: true
                            });

                            console.log( 'Failed to reset password: ', error );

                        });

                }

            });

        };

我试过玩这个preConfirm功能,但这没什么区别。警报不会消失,而是会保留在屏幕上,但仍然没有动画。

我哪里错了?

AccountFactory返回以下函数:

            resetAccount: function( email ) {

                var deferred = $q.defer();

                $http({
                    url: ApplicationConstants.apiServerPath + 'api/users/reset', 
                    method: 'POST', 
                    data: 'email=' + email, 
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                })
                .success( function( data ) {

                    deferred.resolve( data );   

                })
                .error( function( error ) {

                    deferred.reject( error );

                });

                return deferred.promise;

            }
4

1 回答 1

0

您正在寻找正确的方向:preConfirm是您需要在这里使用的。

此代码应该适合您:

swal({
    title: 'Forgot Password?', 
    text: 'Enter your email address and your password will be reset and emailed to you.', 
    input: 'email', 
    showCancelButton: true, 
    confirmButtonText: 'Send', 
    confirmButtonColor: "#DD6B55", 
    inputPlaceholder: 'Email address', 
    showLoaderOnConfirm: true,
    preConfirm: function(email) {
        return AccountFactory.resetAccount(email)
    }
}).then(function( data ) {

    swal({
      title: 'Success!', 
      text: 'A verification email has been sent to ' + email, 
      type: 'success', 
      confirmButtonText: 'Close', 
      allowEscapeKey: false
    });

}, function( error ) {

    swal({
      title: 'Email not found', 
      text: 'Sorry, but we could not find an account matching that email address.', 
      type: 'error', 
      confirmButtonText: 'Close', 
      allowEscapeKey: true
    });

    console.log( 'Failed to reset password: ', error );

});

还可以查看 SweetAlert2 文档中的示例:https ://sweetalert2.github.io/#ajax-request

于 2016-09-02T09:19:46.103 回答