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.