Currently, I am engaged in a project using Angular that focuses on being highly modular. This means that different sections of the application can be enabled or disabled for various clients using Webpack. While this structure has been effective for me, I have encountered an issue related to handling services that may not always be present.
My current approach to this issue is rather straightforward. I utilize $injector.has()
to verify the existence of a service, and if it is present, I utilize $injector.get()
to retrieve it:
function initialize($injector) {
if ($injector.has("barcode")) {
let barcode = $injector.get("barcode");
// Perform actions using the barcode service
}
}
While this method seems to be effective, I have not found extensive information on this particular pattern and any possible drawbacks it may have.
Therefore, my queries are:
- Are there any potential pitfalls to using the injector in this manner that I should take into consideration?
- Is there a more optimal or customary approach to addressing this issue?