Reputation: 167
My application is 80% written in angular and 20% in jquery. I have written a request interceptor in angular using $httpprovider and it is working fine with other angular pages. I have three questions:
1) I want to use the same interceptor for my jquery page. How may I achieve this?
2) I want my interceptor to be called only once at page load. How may I achieve this ? It is presently getting called 7-8 times (I guess the number of ajax calls during the complete page load).
3) Can someone give me inputs how may I write jasmine spec for this interceptor and for the pages using this interceptor. Thanks in advance!
app.config(['$httpProvider', function ($httpProvider) {
'use strict';
$httpProvider.interceptors.push('myAppInterceptor');
}]);
app.factory('myAppInterceptor', ['$q','$window','$injector',function ($q,$window,$injector) {
'use strict';
var myAppInterceptor = {
request: function(config) {
console.log('myAppInterceptor is called');
// some business logic done here...
}
return config;
}
};
return myAppInterceptor;
}]);
Upvotes: 0
Views: 331
Reputation: 4923
1) To have your XHR calls from your legacy code intercepted by your $httpProvider
interceptor you would need to call an angular service that makes the XHR-call through $http
or $resource
.
Se this for info about calling angular services from legacy code.
2) Angular interceptors are ment to be called on every request. If you only want to call it once on page load an interceptor is not the right choice. The better option would make the request + logic you want at page load in a service and have that as a resolve
property on your router state.
3) Use $httpBackend in jasmine to mock $http
calls and se if the interceptor does whatever it is supposed to do.
Upvotes: 1