Having trouble implementing showLoaderOnConfirm feature in SweetAlert2

I’ve been encountering some difficulties with implementing the showLoaderOnConfirm feature using sweetalert2 and ngSweetAlert for AngularJS.

Although the code below runs smoothly and without any errors, I’m not seeing any loading animation. The alert simply disappears and reappears once the request has been processed.

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

I’ve attempted to adjust the preConfirm function, but it doesn’t seem to have much of an effect. Instead of the alert disappearing, it remains on the screen without any animation.

What could be the issue here?

Here is the function that my AccountFactory returns:

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

Answer №1

You're on the right track: you should utilize preConfirm in this scenario.

This sample code should suit your needs:

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

});

Make sure to check out the example in SweetAlert2 documentation as well:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sending data with AngularJS through the $http.post method

I have been attempting to make an $http.post request and receive the response, but despite trying various methods, I am unable to get it to work. The data I am sending (userData) is in JSON format. $http.post(url, userData) .success(function(data, st ...

Make TypeScript parameter optional if it is not supplied

I am working with an interface that defines scenes and their parameters: export interface IScene<R extends string> { path: R; params?: SceneParams; } The SceneParams interface looks like this: export interface SceneParams { [key: string]: s ...

Updating the handler function for AutoComplete with Checkbox in Material UI using React JS

I am looking to include an <AutoComplete /> field in my form. The options for this field are fetched through a get request, and here is the result displayed in the console. [ { "uid": "c34bb0ed-9f63-4803-8639-a42c7e2a8fb0&q ...

Is it possible to transfer information to a state using ui-router?

When calling a state, I currently have the following code: self.$state.go('heat', { subjectId: self.sus.subject.id, examId: self.exs.exam.examId, testId: self.test.testI ...

What are some ways to accomplish this task without relying on a photo editing tool?

Is it possible to swap the image link: https://i.sstatic.net/Wa4mo.jpg with this new link: https://i.sstatic.net/hqVuQ.jpg without having to use a photo editor? Below is the CSS and HTML code: I was unable to upload the logo due to the restrictio ...

Troubleshooting: AngularJS Form Submission Failure

As a novice in Angular and not a JavaScript expert, I am facing challenges with form submission. My task involves loading movie showtimes from an http API and using ng-repeat to display them within a page container. The REST API requires a zipcode input, w ...

Transform an array of string values that occur multiple times into an array of objects organized by their index

Assuming I have a list structured as follows: [ "id1", "01-01-2019", "name1", "€ 5,60", "id2", "02-01-2019", "name2", "€ 5,70", "id3", "03-01-2019", "name3", "€ 5,20", ... ] and my goal is to transform it into a list of JSON objects ...

Sending data from an HTML textarea to a PHP variable without user input

After spending more than two days searching for a solution to this problem, I am now reaching out directly with my question. Although there have been some hints and partial answers, nothing has definitively resolved the issue I am facing, prompting me to m ...

Preventing the triggering of events or functions while utilizing angular-gantt

Currently, I am utilizing the Angular directive called angular-gantt to create a gantt chart that displays tasks. Among the various options available, the ones I am focusing on are on-row-clicked, on-task-clicked, and on-task-updated. The issue I am facin ...

Differentiating between an individual array and an array containing multiple arrays

Can jQuery differentiate between a regular array, an array of arrays, and an array of objects? var a = [1,2,3]; var a2 = [[12,'Smith',1],[13,'Jones',2]]; var a3 = [{val:'12', des:'Smith', num:1}]; //a = regular arr ...

Utilizing Phantom Js with Apache server

After creating a JavaScript app, I realized the importance of making it SEO friendly. I am curious if anyone has experience setting up a crawlable webpage on Apache using Backbone.js (potentially with assistance from PHP and .htaccess files, or with Phant ...

How to keep your vue-router up-to-date

As a newcomer to vuejs, I am using vue cli 3 for my project which consists of various components. One of the components is a menu component where I retrieve all the menu items from an API (code provided below). I have integrated vue-router for routing purp ...

Express: router.route continues processing without sending the request

I've implemented the following code in my Express application: var express = require('express'); // Initializing Express var app = express(); // Creating our app using Express var bodyParser = require(' ...

Using jQuery to encapsulate HTML within a div tag

I am looking to insert some HTML markup into my webpage using jQuery. I want to wrap the highlighted yellow code below with <div class="section">...</div>. Here is an example of what I need: <div class="section"> <div>...< ...

Tips for incorporating a Python script into a online project

I've been working on a Python code that detects faces and eyes using face recognition. When the code runs in PyCharm, it displays a camera window. Now I'm trying to figure out how to integrate this window into a webpage project written in HTML, C ...

Transform JSON time data from Coordinated Universal Time (UTC) to Indian Standard

Hello, I consider myself an amateur in the world of online javascript learning. Currently, I have come across a challenge that has left me stuck. I am working with a JSON time data provided in UTC format (e.g. 16:00:00Z) and my goal is to convert it to IS ...

The process of deploying production-ready code using webpack and angular2

Currently, my Angular 2 code utilizes both webpack and grunt. During development, I rely on webpack-dev-server as a grunt task. When it comes to preparing the code for production deployment, I handle all minification tasks through Grunt which results in t ...

Determining Asynchrony in Node.js Through Programming

Is there a way to assess if a function can be executed asynchronously without requiring a callback? I am currently working with Node.js on the Intel Edison platform and utilizing mraa. The native C++ functions like i2c.readReg(address) do not have provisi ...

"Exploring the process of assigning input data to a different variable within a Vue component

Reviewing the code snippet I currently have: <template> <div> <input v-model.number="money"> <p>{{ money }}</p> </div> </template> <script> name: 'MyComponent', data () { ...

What is the process for obtaining the indirect link of a Google search result?

I am looking to obtain the indirect link of a Google search result. After performing a search on Google, if I right-click on the result link, it changes to something like this: https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&am ...