class
final
Http context stores arbitrary user defined values and ensures type safety without actually knowing the types. It is backed by a Map
and guarantees that keys do not clash.
class HttpContext { set<T>(token: HttpContextToken<T>, value: T): HttpContext get<T>(token: HttpContextToken<T>): T delete(token: HttpContextToken<unknown>): HttpContext has(token: HttpContextToken<unknown>): boolean keys(): IterableIterator<HttpContextToken<unknown>> }
This context is mutable and is shared between cloned requests unless explicitly specified.
Further information is available in the Usage Notes...
set() | ||||||
---|---|---|---|---|---|---|
Store a value in the context. If a value is already present it will be overwritten. | ||||||
|
token | HttpContextToken<T> | The reference to an instance of |
value | T | The value to store. |
HttpContext
: A reference to itself for easy chaining.
get() | |||
---|---|---|---|
Retrieve the value associated with the given token. | |||
|
token | HttpContextToken<T> | The reference to an instance of |
T
: The stored value or default if one is defined.
delete() | |||
---|---|---|---|
Delete the value associated with the given token. | |||
|
token | HttpContextToken<unknown> | The reference to an instance of |
HttpContext
: A reference to itself for easy chaining.
has() | |||
---|---|---|---|
Checks for existence of a given token. | |||
|
token | HttpContextToken<unknown> | The reference to an instance of |
boolean
: True if the token exists, false otherwise.
keys() |
---|
|
// inside cache.interceptors.ts export const IS_CACHE_ENABLED = new HttpContextToken<boolean>(() => false); export class CacheInterceptor implements HttpInterceptor { intercept(req: HttpRequest<any>, delegate: HttpHandler): Observable<HttpEvent<any>> { if (req.context.get(IS_CACHE_ENABLED) === true) { return ...; } return delegate.handle(req); } } // inside a service this.httpClient.get('/api/weather', { context: new HttpContext().set(IS_CACHE_ENABLED, true) }).subscribe(...);
© 2010–2023 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://angular.io/api/common/http/HttpContext