Always do actions before and after $ http

I want my application to always perform some before and after actions $http, performed as loading animations or deleting all messages.

// Add loading animation + Remove all messages
$http({}).then(function(response) {
    // Remove loading animation + Add Success messages
}, function(response) {
    // Remove loading animation + Add Failure mesages
});

But if I code like this, my code is not dry. How can I do this without repetition?

Thank!

+4
source share
2 answers

You should be able to implement it with Interceptors. Read below.

http://www.webdeveasy.com/interceptors-in-angularjs-and-useful-examples/

Interceptor. ON . , DIV ng-if like

<div ng-if="vm.myHttpStateService.isBusy">
..... animation here ...
</div>
0

prom .finally

//Add loading animation + Remove all messages
$http({}).finally(function() {
    //Remove loading animation 
}).then(function onSuccess(response){
    //Add Success messages
}).catch(function onReject(response){
    //Add Failure mesages
});

:

API

  • finally(callback, notifyCallback) - , . , , . . .

- AngularJS $q Service API Reference - API

-1

Source: https://habr.com/ru/post/1651987/


All Articles