在angular.module之前加载javascript文件

Load javascript files before angular.module

本文关键字:加载 javascript 文件 angular module      更新时间:2023-09-26

我想创建一种js库。用户应该只包含一个js文件,他就可以使用我的所有函数。

我用的是angularjs。

在我的angular.module中,我有几个"注射器"(如果不是名称,请更正(定义如下:

var app = angular.module('WDX', ['ui.bootstrap', 'ngRoute', 'ng-currency', 'ngSanitize']);

为了使其工作,我需要在angular JS文件之前包含一些JS文件(如"ng-currency.JS"(。但是,我希望用户只需要包含这个angularjs文件(或者只包含一个其他文件,不再包含(。。。

我试图创建一个新的JS文件来加载angularjs文件之前的所有内容,比如:

$.getScript("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js", function () { });
$.getScript("/Scripts/ng-currency.js", function () { });
$.getScript("/Scripts/kendo.all.min.js", function () { });
$.getScript("/Scripts/placeholder.js", function () { });
$.getScript("/Scripts/angular-sanitize.js", function () { });

但我仍然有这个错误:

angular.js:38未捕获错误:[$injector:modulerr]http://errors.angularjs.org/1.3.12/$injector/modulerr?p0=测试&p1=错误%3A%20…0d%20(http%3A%2F%2localhost%3A46223%2FScript%2Angular.min.js%3A17%3A381(

如果我在angularjs文件之前的html中包含所有脚本,它会起作用,但我不希望这样:/

有什么解决方案吗?

谢谢你的回答!!

您是否尝试过手动引导并在angular.boot之前运行脚本,如下所示?

angular.element(document).ready(function() {
      // add your script here
      angular.bootstrap(document, ['WDX']);
    });

// OR you could add event listener in your app.js as follows:
function onDocumentReady() {
    // add your script here
    angular.bootstrap(document, ["WDX"]);     
}
document.addEventListener('DOMContentLoaded', onDocumentReady, false);
var app = angular.module('WDX', ['ui.bootstrap', 'ngRoute', 'ng-currency', 'ngSanitize']);

注意:在手动引导时从DOM中删除ng应用程序