Every time $injector.get is invoked, it triggers the run() method of the Angular module

I find myself in a situation where I need to manually retrieve objects from the Angular $injector.

Up until now, I have been using the following approach:

var injector = angular.injector(['app.service', 'ng']);
var myService = injector.get('myService');

While this method worked well, I recently encountered an issue. It seems that every time I call angular.injector, the run() method of the app.service module is triggered. This has led to my app initialization logic getting executed multiple times unnecessarily.

Should I consider moving my app bootstrapping code out of the run() method, or is there a way to obtain the $injector without triggering the run() method?

Additionally, I am concerned about the potential performance implications of frequently calling the injector. Is this a valid concern?

Answer №1

The information provided may not be entirely clear, but it is important to note that angular.injector does indeed initialize the entire module including config and run blocks (which helps prevent issues with app components relying on these blocks).

It's also worth mentioning that instances of module services, such as $rootScope, will not be the same as those within a bootstrapped app.

You have the flexibility to organize the module in a way that makes its services easy to use, but do keep in mind that you may not be able to interact with a running Angular app. If you need to use the same code both inside and outside of Angular, it should be defined outside.

If your goal is to interact with a running app, then using

var injector = angular.element(document).injector();

as the documentation suggests.

There might be concerns about performance when calling the injector frequently?

While there may be some RAM and CPU overhead involved, the impact on performance will vary depending on your specific module.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Populating dropdown menu with data fetched from an AJAX request

I've created a dropdown in my cshtml page: @( Html.Kendo().DropDownList().Name("ddlCode").OptionLabel("Select Code...").BindTo(@ViewBag.dropDown) .DataTextField("Title") .DataValueField("domainCode") The dropdown is bound up ...

Server-side props become inaccessible on the client side due to middleware usage

I'm attempting to implement a redirect on each page based on a specific condition using Next.js middleware. Strange enough, when the matcher in middleware.ts matches a page, all props retrieved from getServerSideProps for that page end up being undef ...

Encountered a network error: A rogue token < was found in JSON at position 0 within a new Apollo

https://i.sstatic.net/D25ai.png const httpLink = createHttpLink({ uri: 'http://localhost:3090/' }) const client = new ApolloClient({ link: httpLink, cache: new InMemoryCache() }) client.query({ query: gql` query users { ...

Vanishing Act: React-js MUI Tooltip vanishes upon clicking

The standard behavior of the MUI Tooltip is as follows:
 If the button/icon to trigger a tooltip is not in focus, the tooltip will not disappear when clicking directly on the popper. However, if the button/icon is focused, the tooltip will disappear upo ...

Update image source dynamically using AJAX and jQuery

Within my app, there exists a web page that contains numerous images with dynamically generated source URLs obtained by sending get requests to the rails server. Initially, these images are assigned a default source. Upon loading the page, another request ...

Exploring the Variance between 'npm run serve' and 'npm run dev' Commands in Vue.js Development

Can you explain to me the distinction between npm run serve and npm run dev in vuejs? Additionally, can you clarify why it is recommended to use the npm run serve command when running a project? ...

Ways to align and fix a button within a specific div element

How can I make the button with position: fixed property only visible inside the second div and not remain fixed when scrolling to the first or last div? .one{ height:600px; width: 100%; background-color: re ...

Can you provide the function that updates subscription and account details using Recurly JS?

Recurly has unfortunately declined to provide assistance with this issue. I must understand the specific format of the objects, as Recurly will not process any extra data or incorrect query arguments (which vary for each function). Ruby: subscription = ...

Redirect to a specific webpage depending on a certain condition

I currently have a home page set up and now I would like to create a homePlus page that is controlled by a localStorage variable called alreadyShown. The concept is that once the homePlus page is shown for the first time, we will change the value of alread ...

What is the process for using Selenium and Java to upload a file via an 'input' element with the type "hidden"?

I am facing a challenge with using Java Selenium to upload a file in the input element provided (upload element). <input type="hidden" ng-model="model[options.key || index]" id="formly_21_fileupload_args_content_substrate_surface_media_document ...

Creating a versatile menu component: A step-by-step guide

One of my current projects involves designing a menu screen component that must be user-friendly and easily customizable. The component will consist of various options displayed in list form. const options = [ { text: 'Option 1', }, { ...

What are the steps to integrate jQuery into an Angular 8 application?

I am currently working on an application that relies on SignalR for communication with a desktop application. In order to make use of SignalR, I include jQuery in my .ts file. However, after migrating from Angular 7 to Angular 8, it appears that this setup ...

Utilizing D3.js: Applying Multiple Classes with Functions

My current project involves using D3.js and I've encountered a particular challenge that has me stumped. In the CSV file I'm working with, there are columns labeled "Set" and "Year". My goal is to extract values from these columns and use them a ...

Transferring the AJAX response into a JavaScript variable

New to AJAX and JS here. I am implementing an AJAX code that fetches data from a php service. I am trying to figure out how to store the returned data in a JavaScript variable, which I can then display on the UI. Below is my AJAX code snippet: <script ...

The quirks of JSON.stringify's behavior

I am in the process of gathering values to send back to an ASP.NET MVC controller action. Despite using JSON.stringify, I keep encountering Invalid JSON primitive exceptions and I am unsure why. I have a search value container named searchValues. When I i ...

Eliminate numerous items from an array containing objects

Looking for a way to remove specific objects from an array of objects within another object. I want to delete multiple items simultaneously, but when attempting with splice, it shows up as undefined. This is the snippet of code I'm working with: ...

I encountered an error while attempting to import a file into Firebase Storage

I've been struggling to upload files from Firebase Storage and encountering errors despite reading the documentation and various blogs on the topic. I'm looking for the most effective approach to resolve this issue. import { storage } from ' ...

The requested resource lacks the 'Access-Control-Allow-Origin' header in a basic HTML form

Can someone help me understand why I keep encountering this error in my basic HTML form? I am attempting to display XML data on my website to showcase news stories, but unfortunately, I keep getting stuck with this persistent error. Any assistance would ...

What are the steps for performing a self-triggered AJAX post request?

I have been exploring self-invoked functions and recently used an http.get function to retrieve data from a JSON file like this: var Callmodule = (function(){ var urljsonEntrata= "modello.json"; function getmodules(){ var req = $.ajax({ url: ...

Fetching JSON Data from Web Service using Ajax URL Field - A Simple Guide

I'm facing an issue while attempting to retrieve JSON Data from a web service using ajax url. This is how I have coded it so far: <script type="text/javascript> $('#kategoriTable').dataTable({ ajax: { u ...