You can also use a small service class to do this.
The service just needs to toggle a flag to indicate whether the interceptor should run, and the interceptor class can check the flag before running.
@Injectable()
export class InterceptorOverrideService {
disableInterceptor = false;
constructor() { }
disable() {
this.disableInterceptor = true;
}
enable() {
this.disableInterceptor = false;
}
}
And the Interceptor class:
export class MyInterceptor implements HttpInterceptor {
private count = 0;
constructor(private overrideService: InterceptorOverrideService) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const methods = ['POST', 'PUT', 'PATCH', 'DELETE'];
if (methods.indexOf(req.method) === -1 || this.overrideService.disableInterceptor) {
return next.handle(req);
}
//Your Code here
return next.handle(req).pipe(finalize(() => { //finalize is called for either success or error responses
}));
}
}