Angular2 Bootstrap Parameters from Backend using Dependency Injection
Problem:
In Angular2, how can parameters rendered on the backend be passed to the bootstrap method to set HTTP headers for all requests using BaseRequestOptions?
Solution:
Utilizing Angular's dependency injection, parameters can be passed directly to the bootstrap function:
var headers = ... // retrieve headers from backend bootstrap(AppComponent, [{provide: 'headers', useValue: headers})]);
To inject the headers into components or services, use the @Inject() decorator:
class SomeComponentOrService { constructor(@Inject('headers') private headers) {} }
Alternatively, a custom request options class can be created and injected directly:
class MyRequestOptions extends BaseRequestOptions { constructor (private headers) { super(); } } var values = ... // retrieve headers from backend var headers = new MyRequestOptions(values); bootstrap(AppComponent, [{provide: BaseRequestOptions, useValue: headers})]);
Additional Approaches:
The above is the detailed content of How to Pass Backend-Rendered Parameters to Angular 2 Bootstrap for Setting HTTP Headers?. For more information, please follow other related articles on the PHP Chinese website!