【发布时间】:2016-03-18 03:00:38
【问题描述】:
我正在编写一组 Angular 指令。其中一些将使用其他 3rd Angular 依赖项(例如ngFileUpload、ui-select)。目前我都这样声明:
angular.module('module1', ['ngFileUpload'])
.directive('MyDirective1NeedNgFileUpload', function() {
// ...
});
angular.module('module2', ['ui-select'])
.directive('MyDirective1NeedUiSelect', function() {
// ...
});
angular.module('myLibrary', ['module1', 'module2']);
通常,用户必须在一个真实的应用程序中包含三个 js 文件,如下所示:
<html>
<body>
...
<script src="/path/to/ng-file-upload.js"></script>
<script src="/path/to/ui-select.js"></script>
<script src="/path/to/myLibrary.js"></script>
<script src="app.js"></script>
</body>
</html>
// in app.js
angular.module('myapp', [
'myLibrary',
'ngFileUpload',
'ui-select'
]);
如果我只在应用程序 1 中使用module1,我根本不需要ui-select。但是我还是需要包含ui-select,否则浏览器会报错Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/...。
我想问/做的是,我想延迟加载第三个依赖项,只有在真正需要它们时,或者只是抛出关于缺少什么的更具体的错误消息。
这怎么可能?
【问题讨论】:
标签: javascript angularjs angularjs-directive dependencies