Dynamic controller inject into AngularJS

I need to create dynamic controllers and inject it in my application.

This is my structure:

  1. I have directives for my dynamic HTML code that call a function that create dynamic controller
  2. I inject the dynamic controller into the page
  3. I inject the HTML code into the page and connect the dynamic controller with the HTML code

HTML PAGE:

<html id="ng-app" ng-app"myapp">
<hp:entity mode="onview" name="VIEWS_PhaseOrderSellClose" codeUnique="PhaseOrderSellClose"> ... other html code ...
</openentity>
</html>

APP.JS directive

.directive('hpEntity', function(Artemide){
    /*
    * Non ha mai logica di disegno.
    *
    * <hp:Entity name="" mode="" codeUnique="true/false"></hp:Entity>
    *
    * default:
    *   interface: false
    * */
    return {
        restrict: 'E',
        scope: false,
        transclude: true,
        replace: true,
        template: function(elem, attr){
            //TODO: LUCA, mettere tutti i controlli sui parametri obbligatori
            var _entityObj = {
                name: attr.name,
                mode: attr.mode,
                codeUnique: attr.codeunique,
                interface: false
            }
            var entityControllerName = Artemide.openEntity(_entityObj.name, _entityObj.mode, _entityObj.interface, null, null, null, null, null, null, 'false', false, false, _entityObj.codeUnique);
            var newTag = "<div ng-init='initEntity(null,null,\""+entityControllerName+"\")' ng-controller='" + entityControllerName + "'><replace></replace></div>";

            return newTag;
        },
        link: function(scope, element, attrs, ctrl, transclude){
            element.find('replace').replaceWith(transclude());
        }
    }
})

ARTEMIDE.JS openEntity function

function openEntit(...)  {
var _entityKEY =  openEntity(_parEntityID, _parMode, _parInterface, _parUseGroupKEY, _parFormPanelKEYForPreview, _parEntityKEYCaller, _parXMLOpenEntity, _parMakerKey, _parMakerFather, _parSelectionType, _parExclusive, _parVerifyUnique, _parCodeUnique);

                var _jsCompiled = function($scope, $rootScope, $controller, Artemide, $log) {
                    $scope.initEntity = function(_parFSName, _parPanelKEY, _parEntityKEY) {
                        $log.debug('Artemide da dentro ',Artemide);

                        $scope.ctrlname = _parEntityKEY;
                        $scope.entity = Artemide.globals.OUI_Entity[_parEntityKEY];
                        $scope.foundset =  Artemide.globals['foundsetpointer_'+_parEntityKEY];

                        $log.debug('[LOOK_ME] foundset: ',$scope.foundset);
                    }
                }; /**/

                angular.module('main1').controller(_entityKEY, _jsCompiled);

                registerController('main1', _entityKEY);

                return _entityKEY;

}



function registerController(moduleName, controllerName, template, container) {
    console.log('>>>>> REGISTER CONTROLLER: '+controllerName+ ' to '+moduleName)
    // Load html file with content that uses Ctrl controller
   // $(template).appendTo(container);
    // Here I cannot get the controller function directly so I
    // need to loop through the module's _invokeQueue to get it
var queue = angular.module(moduleName)._invokeQueue;
for(var i=0;i<queue.length;i++) {
    var call = queue[i];
    if(call[0] == "$controllerProvider" &&
        call[1] == "register" &&
        call[2][0] == controllerName) {
        controllerProvider.register(controllerName, call[2][1]);
    }
}

angular.injector(['ng', moduleName]).invoke(function($compile, $rootScope) {
    $compile(controllerName)($rootScope);
    $rootScope.$apply();
});
}

When I launch my app the first time the dynamic controller and HTML content works correctly but when I open the second link that request a new dynamic controller and the new HTML code I receive this error

Error: [ng:areq] http://errors.angularjs.org/1.4.0-beta.2/ng/areq?p0=g2_views_phaseordersellclose_2&p1=not%20aNaNunction%2C%20got%20undefined

I have NO error during the $compile and $inject function.

Any ideas?

Thanks