0

I'm wondering if it's possible to inject a controller into a .run

Here is what I am trying

 .run([
        multistateModule.componentName,
        function($controller, $scope, MultiState) {
            $controller('builderProductCtrl.componentName', {
                $scope: controllerTest
            });
            console.log(controllerTest);

        }
    ]);

Forgive the requirejs format, builderProductCtrl.componentName is the controllers name. Angular does not seem to like this, and it is telling me controllerTest is undefined in the js hint. I'm wondering if something like this is possible? I want to inject it and use a function from the controller in the run. Thanks!

EDIT: can't use scope in run as far as I can tell, so I swapped to rootscope - see here :

.run([
        '$controller',
        '$rootScope',
        multistateModule.componentName,
        builderProductCtrl.componentName,
        function($controller, $rootScope, MultiState, builderCtrl) {
            var controllerTest = $rootScope.$new();
            $controller('builderCtrl', {
                $rootScope: controllerTest
            });
            console.log(controllerTest);
        }

When I do this I get an unknown provider for the controller (builderProductCtrlProvider), maybe it's possible to use the $controllerProvider - https://docs.angularjs.org/api/ng/provider/$controllerProvider , I'm just not sure how to apply the logic in the documentation.

I will try and add a little more clarity -

I have a custom routing module I created to route an angular app in a requirejs format (where each item is it's own angular/requirejs format). It is set up to fire a callback into the module that changes which is registered in the .run. Probably, the easiest way to accomplish this is to make a service/factory that handles and a listener of some sort in the controller that polls for changes.

Basically it boils down to $scope variables that change based off of the routing state, so I was thinking for a moment - if I could just inject a function from inside the controller it would skip the step of the service/factory and the callback would just call that function inside the controller. It seems more like a provider with a broadcast/emit is the better idea, but I was wondering in general if it was even possible to inject the controller. Something like this How do I inject a controller into another controller in AngularJS, but into the run instead of another controller. Hope this helps! Thanks.

Community
  • 1
  • 1
ajmajmajma
  • 13,712
  • 24
  • 79
  • 133
  • 1
    not sure why you would want to do this – Michael Kang May 19 '15 at 02:53
  • @pixelbits yeah I know It would probably be best to just use a service/factory, however this has lead me to being curious if it is even possible. – ajmajmajma May 19 '15 at 02:54
  • This sounds like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What are you actually trying to achieve? – Phil May 19 '15 at 03:08
  • @Phil I added a bit more explanation for clarity, really I guess I'm just wondering if it's possible. – ajmajmajma May 19 '15 at 16:30
  • Possible duplicate of [Inject dependencies in "run" method of the module in Angularjs](http://stackoverflow.com/questions/14688290/inject-dependencies-in-run-method-of-the-module-in-angularjs) – Paul Sweatte Nov 28 '15 at 21:53

0 Answers0