Self explanatory fiddle: http://jsfiddle.net/5FG2n/6/
I need to dynamically choose the controller to use at runtime based on its name as a string. The string will b
You don't need to dynamically choose the controller. Use a single controller and use DI to provide different functionality to that controller.
angular.module('app', [])
.service('ControllerService', function() {
this.get = function(controllerName) {
// ... do your logic for returning the controller functionality here
if (controllerName == "fooController") {
return {
foo: function() { return "foo" }
}
}
else {
return {
foo: function() { return "bar" }
}
}
});
})
.controller('SingleController', ['$scope', 'ControllerService',
function($scope, ControllerService) {
$scope.functionality = ControllerService.get('fooController');
});
You bind to the functionality
object on your controller:
{{functionality.foo()}}