How to make directive use the controller specified in directive attribute?

So I have a directive:

<directive data="user" templateUrl="./user.html" controller="UserController"></directive>

I want that directive to use the controller specified in “controller” attribute, as you see above.

Is it possible with AngularJS directives? Or should I do it other way, maybe with components?

My code currently looks like this:

app.directive('directive', function() {
        
        var controllerName = "UserController"; // i want that to dynamicaly come from attribute
        
        // check if controller extists:
        var services = [];
        app['_invokeQueue'].forEach(function(value){ 
            services[value[2][0]] = true;
        });         
        if (!services[controllerName]) controllerName = false;
        
        return {
            
            scope: { 'data' : '=' },

            link: function (scope) {
                
                Object.assign(scope, scope.data);
            },
            
            templateUrl: function(element, attr) {
                
                return attr.templateurl;
            },
            
            controller: controllerName
            
        }
        
    });

Answer

Okay, so after analysing Petr’s answer I post the working code using nested divs:

app.directive('directive', function() {
        
        return {
            scope: { 'data' : '=' },
            
            link: function (scope) {
                
                // this makes your fields available as {{name}} instead of {{user.name}}:
                Object.assign(scope, scope.data);
                
            },
            
            template: function(element, attrs) {
                
                var controllerName = attrs.controller;
                var controllerString = controllerName + ' as vm';
                
                // check if controller extists:
                var services = [];
                app['_invokeQueue'].forEach(function(value){ 
                    services[value[2][0]] = true;
                })
                
                if (!services[controllerName]) {
                    
                    return '<div ng-include="'' + attrs.templateurl + ''"></div>'; 
                    
                } else {
                    
                    return '<div ng-controller="' + controllerString + '"><div ng-include="'' + attrs.templateurl + ''"></div></div>';
                        
                }
            }
        }
        
    });

Leave a Reply

Your email address will not be published. Required fields are marked *