An EnvironmentSource is a service from which to fetch environment variables into a browser synchronously or asynchronously. It can be a constant, a file, a browser storage, a property server, a WebSocket stream or any other type of source that the browser has access to.
An application can have as many sources as it needs, and they all have access to the EnvironmentState to build calls, wait for specific properties or other sources, etc. How these sources are resolved or how they add the properties to the environment can be defined by the source properties.
The EnvironmentSource is an interface that must be implemented to obtain environment properties.
The ENVIRONMENT_SOURCES injection token is used to set the sources from which to get environment properties. The sources can be setted as an injectable Class, a plain object or an object instance.
If there is a single source inject using useClass.
In order to be able to override the ENVIRONMENT_SOURCES injection token in feature modules and allow the use of multiple sources, the module provides the ENVIRONMENT_SOURCES_FACTORY injection token, that uses a factory to inject all source objects in an Array. If the provided class is not injectable the factory will throw. This is the token used by the environment loader.
Below are examples of the expected behavior and some implementation examples. To learn more about environment sources and how to create them you can read the documentation.
If a feature module need a special set of environment properties can define a specific set of sources using the ENVIRONMENT_SOURCES injection token or EnvironmentModule.forChild(), as described in the EnvironmentModule documentation.
Angular Environment Sources
An EnvironmentSource is a service from which to fetch environment variables into a browser synchronously or asynchronously. It can be a constant, a file, a browser storage, a property server, a WebSocket stream or any other type of source that the browser has access to.
An application can have as many sources as it needs, and they all have access to the EnvironmentState to build calls, wait for specific properties or other sources, etc. How these sources are resolved or how they add the properties to the environment can be defined by the source properties.
The EnvironmentSource is an interface that must be implemented to obtain environment properties.
ENVIRONMENT_SOURCES
The
ENVIRONMENT_SOURCES
injection token is used to set the sources from which to get environment properties. The sources can be setted as an injectable Class, a plain object or an object instance.If there is a single source inject using
useClass
.If there are multiple sources inject an Array using
useValue
. The array can contain injectable classes orEnvironmentSource
instances.The default value provided by
EnvironmentModule.forRoot()
isnull
, but can be set in configuration using:ENVIRONMENT_SOURCES_FACTORY
In order to be able to override the
ENVIRONMENT_SOURCES
injection token in feature modules and allow the use of multiple sources, the module provides theENVIRONMENT_SOURCES_FACTORY
injection token, that uses a factory to inject all source objects in an Array. If the provided class is not injectable the factory will throw. This is the token used by the environment loader.Use cases
Below are examples of the expected behavior and some implementation examples. To learn more about environment sources and how to create them you can read the documentation.
Table of Contents
Feature Module Sources
If a feature module need a special set of environment properties can define a specific set of sources using the
ENVIRONMENT_SOURCES
injection token orEnvironmentModule.forChild()
, as described in theEnvironmentModule
documentation.The default value provided by
EnvironmentModule.forChild()
isnull
, but can be set in configuration.Load the static environment of Angular