本文介绍了动态加载 AngularJS 控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有页面,我需要将带有可以动态加载的控制器的 Angular 应用程序放入其中.

I have an existing page into which I need to drop an angular app with controllers that can be loaded dynamically.

这是一个片段,它根据 API 和我发现的一些相关问题实现了我对如何完成它的最佳猜测:

Here's a snippet which implements my best guess as to how it should be done based on the API and some related questions I've found:

// Make module Foo
angular.module('Foo', []);
// Bootstrap Foo
var injector = angular.bootstrap($('body'), ['Foo']);
// Make controller Ctrl in module Foo
angular.module('Foo').controller('Ctrl', function() { });
// Load an element that uses controller Ctrl
var ctrl = $('<div ng-controller="Ctrl">').appendTo('body');
// compile the new element
injector.invoke(function($compile, $rootScope) {
    // the linker here throws the exception
    $compile(ctrl)($rootScope);
});

JSFiddle.请注意,这是对实际事件链的简化,上面几行之间有各种异步调用和用户输入.

JSFiddle. Note that this is a simplification of the actual chain of events, there are various async calls and user inputs between the lines above.

当我尝试运行上述代码时,$compile 返回的链接器抛出:Argument 'Ctrl' is not a function, got undefined.如果我正确理解 bootstrap,它返回的注入器应该知道 Foo 模块,对吗?

When I try to run the above code, the linker which is returned by $compile throws: Argument 'Ctrl' is not a function, got undefined. If I understood bootstrap correctly, the injector it returns should know about the Foo module, right?

如果我使用 angular.injector(['ng', 'Foo']) 创建一个新的注入器,它似乎可以工作,但它会创建一个新的 $rootScope 不再与引导 Foo 模块的元素的作用域相同.

If instead I make a new injector using angular.injector(['ng', 'Foo']), it seems to work but it creates a new $rootScope which is no longer the same scope as the element where the Foo module was bootstrapped.

我是否使用了正确的功能来执行此操作,还是我遗漏了什么?我知道这不是 Angular 的方式,但我需要将使用 Angular 的新组件添加到不使用 Angular 的旧页面,而且我不知道引导模块时可能需要的所有组件.

Am I using the right functionality to do this or is there something I've missed? I know this isn't doing it the Angular way, but I need to add new components that use Angular to old pages that don't, and I don't know all the components that might be needed when I bootstrap the module.

更新:

我已经更新了 fiddle 以表明我需要能够在不确定的点向页面添加多个控制器及时.

I've updated the fiddle to show that I need to be able to add multiple controllers to the page at undetermined points in time.

推荐答案

我找到了一个可能的解决方案,在引导之前我不需要了解控制器:

I've found a possible solution where I don't need to know about the controller before bootstrapping:

// Make module Foo and store $controllerProvider in a global
var controllerProvider = null;
angular.module('Foo', [], function($controllerProvider) {
    controllerProvider = $controllerProvider;
});
// Bootstrap Foo
angular.bootstrap($('body'), ['Foo']);

// .. time passes ..

// Load javascript file with Ctrl controller
angular.module('Foo').controller('Ctrl', function($scope, $rootScope) {
    $scope.msg = "It works! rootScope is " + $rootScope.$id +
        ", should be " + $('body').scope().$id;
});
// Load html file with content that uses Ctrl controller
$('<div id="ctrl" ng-controller="Ctrl" ng-bind="msg">').appendTo('body');

// Register Ctrl controller manually
// If you can reference the controller function directly, just run:
// $controllerProvider.register(controllerName, controllerFunction);
// Note: I haven't found a way to get $controllerProvider at this stage
//    so I keep a reference from when I ran my module config
function registerController(moduleName, controllerName) {
    // 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]);
        }
    }
}
registerController("Foo", "Ctrl");
// compile the new element
$('body').injector().invoke(function($compile, $rootScope) {
    $compile($('#ctrl'))($rootScope);
    $rootScope.$apply();
});

小提琴.唯一的问题是您需要存储 $controllerProvider 并在真正不应该使用它的地方(在引导程序之后)使用它.此外,在注册之前,似乎没有一种简单的方法可以获取用于定义控制器的函数,因此我需要遍历模块的 _invokeQueue,这是未记录的.

Fiddle. Only problem is that you need to store the $controllerProvider and use it in a place where it really shouldn't be used (after the bootstrap). Also there doesn't seem to be an easy way to get at a function used to define a controller until it is registered, so I need to loop through the module's _invokeQueue, which is undocumented.

更新:要注册指令和服务,而不是 $controllerProvider.register 只需使用 $compileProvider.directive$provide.factory 分别.同样,您需要在初始模块配置中保存对这些的引用.

UPDATE: To register directives and services, instead of $controllerProvider.register simply use $compileProvider.directive and $provide.factory respectively. Again, you'll need to save references to these in your initial module config.

UDPATE 2: Here's a fiddle 自动注册所有控制器/无需单独指定即可加载指令/服务.

UDPATE 2: Here's a fiddle which automatically registers all controllers/directives/services loaded without having to specify them individually.

这篇关于动态加载 AngularJS 控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:58