Retrieve the controller function in AngularJS and then pass it as an argument to $injector.invoke() for execution

Initially, I had set up two controllers in the following way:

function ControllerA(DependencyA, DependencyB) {
    // code
}

function ControllerB(DependencyC, DependencyD) {
    // code
    $injector.invoke(ControllerA);
}

However, for minification purposes, I made changes to the controller definitions as shown below:

myApp.controller('controllerA', ['DependencyA', 'DependencyB' 
    function (DependencyA, DependencyB) {
    // code
}]);


myApp.controller('controllerB', ['DependencyC', 'DependencyD']
    function (DependencyC, DependencyD) {
        // $injector.invoke(controllerA) // ReferenceError: controllerA is not defined 
}]);

The issue here is that $injector.invoke() requires a function as an argument, so the question arises on how to pass controllerA to $injector.invoke()?

Answer №1

If you're looking to create a controller in AngularJS, you have the option to utilize the $controller service. This service can be invoked using a string like so:

$controller('myController' /*, additional parameters */);

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

"Encountering a duplicate key error when performing a bulk upsert operation with LoopbackJS and MongoDB

Attempting to perform batch upserts of multiple documents at once using a PUT request to a loopback-based Rest API. The request body consists of an array of JSON objects. [ {"_id" : "1", "data" : "foo" }, {"_id" : "2", "data" : "bar" ...

Unveiling the secret to accessing the table itself from dtOptions in AngularJS-Datatables

I'm trying to create a download button for a CSV file, but I need to exclude specific columns from the table. I have discovered an option called "exportOptions" that is used in jQuery DataTables to define which columns should be exported. It seems th ...

Generate a hyperlink within a paragraph

Can anyone provide tips on how to turn a string from Json into a paragraph with a hyperlink included? <p>Dumy Dumy Dumy Dumy Dumy Dumy Dumy DumyDumyDumyDumy abc.com </p> Currently, the paragraph displays as is, but I would like to make abc.c ...

Showing ng-attributes in haml partials in Rails using jQuery ajax and $q

As I work on developing an Angular Frontend for an existing Rails Application, I have come across the challenge of integrating $q in my current setup. While I understand that transitioning to a REST API served directly to ngResource would be ideal, the com ...

Using AngularJS routing with an Express 4.0 backend API

Recently, I began working on an application utilizing Express 4.0 server. Following a tutorial on scotch.io (http://scotch.io/tutorials/javascript/build-a-restful-api-using-node-and-express-4), I structured the routes to support a backend api serving an An ...

angular-ui-router nested states do not support multiple views functionality

I am struggling to get nested views to work properly. I have a parent view with navigation and content, and when a user clicks on the navigation links, it should load the correct view. However, for some reason, it is still showing the default content. The ...

What is the best way to pass $scope variables to a controller?

I'm currently in the process of developing a system that involves listing items with specific attributes. Title Skills Budget Date Posted Additionally, I have implemented a modal that opens up when an item is clicked in the list. My goal is to retr ...

Holding out for a callback response in AngularJS / Ionic is key

Is there a way to delay the return of the result until after receiving a callback response? I am working with Ionic and AngularJS. ...

Problem with applying ng-class directive in AngularJS

I am currently developing an application using AngularJs and Bootstrap. My goal is to display the title of a 'scenario' with different styling based on its status, along with a specific icon. Initially, I attempted the following approach: <s ...

Concealing Components using Angular's ng-change Feature

I need help displaying or hiding an element in a form using ng-change or any other method you suggest. Here is the HTML snippet I am working with: <div ng-app ng-controller="Controller"> <select ng-model="myDropDown" ng-change="changeState( ...

Having trouble establishing a connection between an Ionic app and MySQL through a Wamp server

Hey there, I'm just getting started with the ionic framework and I'm attempting to connect MySQL from my ionic app using Wamp server. However, I seem to be having some trouble with the connection. Here is a snippet of my controller: JS: $scope. ...

Guidance that utilizes the scope of a specific instance

I have successfully created a d3.js directive, but I am facing an issue when resizing the window. The values of my first directive seem to be taking on the values of my second directive. How can I separate the two in order to resize them correctly? (both ...

Troubleshooting the issue of Angular 2 error: Cannot access the 'getOptional' property

Currently, I am navigating my way through angular 2 and attempting to define a service named searchservice. I want to inject this service in the bootstap part of my project: import {SearchService} from 'src/service'; Here is the code for the Se ...

What sets apart $document from $window.document in Angular?

After experimenting with the code on JSBin, I noticed that the first snippet successfully retrieved the canvas object, while the second one did not. JSBin: http://jsbin.com/natavejasa/1/edit?html,js,output var canvas = $window.document.getElementById(&ap ...

The top choice for AngularJS: A trustworthy JSON viewer and editor module

Currently, I am working on enhancing my app by incorporating a json editor feature. I would appreciate any recommendations on which module you have experience with and believe is both stable and effective. The data I am working with is already in json for ...

Having trouble getting an Angular 2.0 service call to pass through the http-proxy-middleware

I recently implemented http-proxy-middleware into my Angular 2.0 application by adding the following configuration in bs-config.js: var proxyMiddleware = require('http-proxy-middleware'); module.exports = { server: { port: 3000, ...

Utilize AngularJS to enable the forward and back buttons to fetch JSON data, but ensure that the data is only retrieved if the item meets

I have successfully implemented forward and back buttons for paging through data items in an array from a JSON file: Controller: dishControllers.controller('DrinkcardsController', ['$scope','$http','$routeParams', ...

Issue with AngularJS UI Router not loading the inline template and controller

I am trying out UI Router for the first time in my AngularJS project. I am facing an issue where, when I click on a link to view a post, it doesn't display. The post template is not visible and I remain on the home page. The URL flashes as http://loc ...

AngularJs Redirect

Here is the code snippet I'm working with: $scope.insertTodo = function(){ TodoService.post($scope.todo); $location.path("/#/"); } The intention is for it to execute TodoService.post() and ...

What is the proper way to configure the checklist-model directive in my AngularJS application?

I'm new to the node.js/grunt world, so please bear with me if my question seems basic... I have an angular.js project set up with yeoman/grunt, and now I want to add a directive, specifically this one. However, I'm unsure of how to install it! ...