动态控制器注入AngularJS

时间:2015-02-09 10:27:21

标签: javascript angularjs dynamic code-injection controllers

我需要创建动态控制器并将其注入我的应用程序。

这是我的结构:

  1. 我有动态HTML代码的指令,它调用一个创建动态控制器的函数
  2. 我将动态控制器注入页面
  3. 我将HTML代码注入页面并将动态控制器与HTML代码连接起来
  4. 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('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 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();
    });
    }
    

    当我第一次启动应用程序时动态控制器和HTML内容正常工作但是当我打开请求新动态控制器和新HTML代码的第二个链接时,我收到此错误

      

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

    我在$ compile和$ inject函数中没有错误。

    有什么想法吗?

    由于

0 个答案:

没有答案