AngularJS - Get controller function by line

I use the ng-include directive, which will have a dynamic template and a controller based on some variables in the scope. I currently have such a card.

 $scope.map = { 'key1': { controller: Ctrl1, templateUrl: 'tmpl1.html' }, 'key12': { controller: Ctrl2, templateUrl: 'tmpl1.html' } } ... <div ng-include src="map[key].templateUrl" ng-controller="map[key].controller"></div> 

However, I would like to discard this map and instead generate templateUrl and the controller via a string. The following returns a controller object, but I need a function that needs to be returned.

 var ctrlObj = angular.module('moduleName').controller('ControllerName') 

EDIT 1

To clarify:

However, I would like to discard this map and instead generate templateUrl and the controller via the line

Basically, I would like to configure the "subcontrollers" on the page so that it is conditional on the configuration. The main controller, which has the information that all subcontrollers would share: FooCtrl will be the "main" controller, while FooBarCtrl, FooBarSubCtrl will be subcontrollers. My goal would be to create a function that resolves "Bar" to "FooBarCtrl" and this would be enough for the corresponding controller function.

+4
source share
3 answers

I do not see a trivial way to achieve this, but I can imagine two possible solutions:

  • Change the ngInclude directive so that it accepts the src expression, parse it to get the name of the controller (template Foo.html => controller FooCtrl) and install the controller on the element, as is done in ngView ( https://github.com/angular/ angular.js / blob / master / src / ng / directive / ngView.js # L149 ):

     controller = $controller(current.controller, {$scope: lastScope}); element.contents().data('$ngControllerController', controller); 
  • Create a new directive, for example. "ajpaz-controller", which follows the src expression and associates the controller with the element in the same way as in step 1. The obvious advantage is that this can be done without changing the original ng-include. This will work as follows

     <div ng-include src="templateUrl" ajpaz-controller> 

Therefore, you must save the current templateUrl template in your parent controller. Hope this makes sense;)

+2
source

I know this question has been posted a long time ago, but it may be helpful for others to know the correct answer ...

If you signed up using regular means using $ controllerProvider:

 app.controller('myController', function() { //blah, blah }); 

You can use the $ controller service to retrieve the controller function, it acts like a getter:

 $controller('myController'); 

Of course, do not forget to enter it ...; -)

+1
source

From the following stackoverflow link. I believe that he has the answer to this question.

Using Angular controllers created using angular.module (). controller ()

Given:

 angular.module('App.controllers', []) .controller('home', function () { $scope.property = true; }]); 

Answer:

 // Try using a string identifier. routeProvider.when('/', {templateUrl: 'partials/home.html', controller: 'home'}); 
0
source

All Articles