javascript - In AngularJS is there such a thing as a finally that runs after $http? -


my code looks this:

  $scope.login = function (username, password, rememberme) {         authentication.authenticating = true;         var config = {             method: 'post',             url: '/api/account/login',             data: { 'username': username, 'password': password, 'rememberme': rememberme }          };         $http(config)             .success(function (data) {                 authentication.authenticating = false;                 authentication.isauthenticated = true;                 $scope.template = $scope.templates[1];                 $scope.username = username;             })             .error(function (data) {                 $scope.loginerror = "invalid username/password combination";                 authentication.authenticating = false;             });     }; 

is there way can move authentication.authenticating = false; code block execute after error or success ?

yes, since $http returns promise, can call .finally(callback) method described in $q documentation https://docs.angularjs.org/api/ng/service/$q

so can use in case this

$scope.login = function (username, password, rememberme) {     authentication.authenticating = true;     var config = {...};      $http(config)         .success(function (data) {             authentication.isauthenticated = true;             $scope.template = $scope.templates[1];             $scope.username = username;         })         .error(function (data) {             $scope.loginerror = "invalid username/password combination";         })         .finally(function() {             authentication.authenticating = false;         }); }; 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -