0

I am trying to retrofit spinners into my app.

I'm expecting to set a loading=true variable when I start async events, and set it false when the call returns.

Then in my view I can do

<span><i class="fa fa-spinner" if-show="vm.loading"></i><span>

I was hoping to find async calls of the form success, failure, finally.

The first controller I opened up makes a call in a form I don't understand. I don't even know what to call it, so I have no idea how to research and explore it.

    $scope.login = function () {
        if ($scope.form.$valid) {
            authService.login($scope.loginData).then(function (response) {
            $location.path("/dashboard");
        },
            function (err) {
                toastr.error(err.error_description);
            });
        }
    };

What I see here is an if statement, followed by a comma, followed by a function.

Uhh... is that some form of try/catch I've not encountered before?

I can't just add a finally on the end...

The reason I'm asking the question here is because I don't even know how to research this.

Ultimately the question I'm trying to answer is: what form of async call can I use so that I have a place to first activate the spinner, and then deactivate it?

DaveC426913
  • 2,012
  • 6
  • 35
  • 63

1 Answers1

0

Ah. OK. It's a standard promise - just confusingly formatted. I overlooked the .then that's on the same line.

$scope.login = function () {
    if ($scope.form.$valid) {
        $scope.loading = true;
        authService.login($scope.loginData)
            .then(function (response) {
                 $location.path("/dashboard");
             },
             function (err) {
                 toastr.error(err.error_description);
             })
             .finally(function(){
                 $scope.loading = false;
             }
       );
    }
}

Found it here:

How to always run some code when a promise is fulfilled in Angular.js

Community
  • 1
  • 1
DaveC426913
  • 2,012
  • 6
  • 35
  • 63