如果需要注射模块动态中,只有 [英] Inject module dynamically, only if required
问题描述
我在Angular.js组合使用Require.js。
I'm using Require.js in combination with Angular.js.
有些控制器需要哪些人不需要,例如巨大的外部依赖性, FirstController
需要的角UI codeMIRROR 。这是一个额外的135 KB,至少有:
Some controllers need huge external dependencies which others don't need, for example, FirstController
requires Angular UI Codemirror. That's a extra 135 kb, at least:
require([
"angular",
"angular.ui.codemirror" // requires codemirror itself
], function(angular) {
angular.module("app", [ ..., "ui.codemirror" ]).controller("FirstController", [ ... ]);
});
我不希望有包括指令和codeMIRROR的lib我每次页面加载只是为了让角高兴。结果
这就是为什么我现在加载控制器只有当路由被击中,像什么的在这里完成一>
然而,当我需要的东西像
However, when I need something like
define([
"app",
"angular.ui.codemirror"
], function(app) {
// ui-codemirror directive MUST be available to the view of this controller as of now
app.lazy.controller("FirstController", [
"$scope",
function($scope) {
// ...
}
]);
});
我怎么能告诉角在应用程序模块藏汉注入 UI。codeMIRROR
模块(或任何其它模块)?结果
我不在乎,如果它要完成这样的hackish的方式,除非涉及到修改外部依赖的code。
How can I tell Angular to inject ui.codemirror
module (or any other module) in the app module aswell?
I don't care if it's a hackish way to accomplish this, unless it involves modifying the code of external dependencies.
如果它是有用的:我跑角1.2.0
推荐答案
我一直在试图混合requirejs +角有一段时间了。我在Github上(角需要懒)用我的努力出版了一本小项目,到目前为止,因为范围太大直列code或小提琴。该项目体现了以下几点:
I have been trying to mix requirejs+Angular for some time now. I published a little project in Github (angular-require-lazy) with my effort so far, since the scope is too large for inline code or fiddles. The project demonstrates the following points:
- AngularJS模块是延迟加载的。
- 指令可以偷懒装入了。
- 有一个模块的发现和元数据的机制(请参阅我的其他宠物项目:需要懒)
- 应用程序被自动分割成捆(即建筑r.js作品)
它是如何做的:
- 的提供者(例如
$ controllerProvider
,$ compileProvider
)是从配置捕获
功能(技术我<一个第一次看到href=\"https://github.com/matys84pl/angularjs-requirejs-lazy-controllers\">angularjs-requirejs-lazy-controllers). - 的Bootstrap后,
角
是由我们自己的包装,可以处理懒加载的模块更换。 - 注射器被捕获并承诺提供。
- AMD的模块可以转换为角模块。
- The providers (e.g.
$controllerProvider
,$compileProvider
) are captured from aconfig
function (technique I first saw in angularjs-requirejs-lazy-controllers). - After bootstraping,
angular
is replaced by our own wrapper that can handle lazy loaded modules. - The injector is captured and provided as a promise.
- AMD modules can be converted to Angular modules.
这实现满足您的需求:它可以延迟加载角模块(至少我现在用的NG-网格),绝对的hackish :)并且不修改外部库
This implementation satisfies your needs: it can lazy-load Angular modules (at least the ng-grid I am using), is definitely hackish :) and does not modify external libraries.
评论/意见欢迎。
(编辑)从别人这种解决方案的区别在于,它没有做动态要求()
通话,从而可以用r.js建(和我的需要-lazy项目)。其他比该想法是在各种溶液或多或少会聚
(EDIT) The differentiation of this solution from others is that it does not do dynamic require()
calls, thus can be built with r.js (and my require-lazy project). Other than that the ideas are more or less convergent across the various solutions.
祝大家!
这篇关于如果需要注射模块动态中,只有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!