0

I have controller defined as this:

var servicesInstance = angular.module('myApp');
servicesInstance.controller('ServicesViewController', function() {
    console.log("ServicesViewControllerconstructor."); 

    this.initialize = function() {
        console.log("ServicesViewController initialize."); 
    };

});    

And I would like to get an instance of it dynamically in code like this:

var instance = $injector.get('ServicesViewController');

Note: please ignore the hardcoded strings. For this post, I hardcoded the strings. however, in reality, the controller name will come from configuration data (eg: json object).

The instance variable is always null. $injector.has('ServicesViewController') always returns false.

Why would this be the case? Is this the correct way to get a controller instance?

Thank you Matt

EDIT: Throwing in more info I kept out because I was trying to keep the question simple. I am using requireJS to load the javascript code which creates the controller. EG:

define([], function(ServicesViewControllerProvider) {
    'use strict';

    var servicesInstance = angular.module('myApp');
    servicesInstance.controller('ServicesViewController', function() {
        console.log("ServicesViewController Constructor"); 

        this.initialize = function() {
            console.log("ServicesViewController initialize."); 
        };

    }); 

    var servicesViewController = new Object();
    servicesViewController.$inject=['$scope'];
    servicesViewController.view = "/test/ServicesView.html";
    servicesViewController.requiresUI = true;    
    servicesViewController.controllerName = "ServicesViewController";    

    return servicesViewController;    
});

Thanks Matt

4
  • 1
    Actually that is not a controller its a service Commented Jul 25, 2017 at 20:18
  • see change in code. sorry. I am trying to get a controller not a service. But should it matter when calling $injector.get? Commented Jul 25, 2017 at 20:19
  • var instance = $controller(''ServicesViewController", {}); Commented Jul 25, 2017 at 20:22
  • If you don't need to have the controller registered first it might be easier to actually do this dynamically. angular.module("myApp").controller( var, function) Commented Jul 25, 2017 at 20:25

1 Answer 1

1

This is one way i know to get an instance of a controller

var instance = $controller('ServicesViewController');

parsing scope it is usually

//var instance = $controller('ServicesViewController',{$scope: $scope});
//call in service
servicesInstance.service('myService',function ($controller, $rootScope) {
   var scope = $rootScope.$new(true);
    //here you have  ServicesViewController scope
   var instance = $controller('ServicesViewController',{scope: $scope});

});
Sign up to request clarification or add additional context in comments.

8 Comments

when I tried that earlier, I was getting an error on $scope. Uncaught TypeError: Cannot read property '$scope' of undefined. I tried defining the controller both with and without $scope injected.
Where you calling it in another controller or a service ?
Henry, can you point me to an example of how to use $modal to set scope? thank you.
Have you tried that new edit to set controller scope?
Thank you for the example, Henry. When I follow that, I get an unknown provider error: Error: [$injector:unpr] Unknown provider: $modalProvider <- $modal Im not familar with $modal. when I google, I get details about modal dialog services which doesn't sound like what we are discussion.... thnx
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.