:::: MENU ::::

Interceptors in Angular.js

Today we will focus how to use interceptor in angular.js to create a control mechanism for http requests and responses.

Lets start coding, as a first step i’ve created interceptor object with request , response and responseError functions you can find code below.

In interceptor object we defined what we want to to in request, response and responseError situations, now we will create a basic angular application like below.

And sample controller with http request

We will push interceptor object to httpProvider.interceptors like below code in angular config.

Now we are able to control every request and response in angular app.

Lets create a index.html file to bind data to html.

index.html  file code :

app.js final code :

Open index.html in browser and check console to see messages.In console first object will have request information and other items are coming from http response. You can check below screen.

interceptorScreen

You can find source code here : https://github.com/hamdiceylan/AngularInterceptorSample

I hope you enjoy, see you on next tutorial.


So, what do you think ?