[英]Load javascript files before angular.module
I want to create a sort of js library.我想创建一种 js 库。 The user should just include ONE js file and he can use all my functions.
用户应该只包含一个 js 文件,他可以使用我的所有功能。
I'm using angularjs.我正在使用 angularjs。
In my angular.module, I have several "Injectors" (correct me if it's not the name) define as follow :在我的 angular.module 中,我有几个“注入器”(如果不是名称,请纠正我)定义如下:
var app = angular.module('WDX', ['ui.bootstrap', 'ngRoute', 'ng-currency', 'ngSanitize']);
To make it working, I need to include some JS files (like "ng-currency.js") before the angular js file.为了使它工作,我需要在 angular js 文件之前包含一些 JS 文件(如“ng-currency.js”)。 BUT, I want the user has just to include this angularjs file (or just ONE other, no more)...
但是,我希望用户只需包含此 angularjs 文件(或仅包含其他一个,不再包含)...
I tried to create a new JS file to load everything before my angularjs file, like that :我尝试创建一个新的 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 () { });
But I still have this error :但我仍然有这个错误:
angular.js:38 Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.12/ $injector/modulerr?p0=test&p1=Error%3A%20…0d%20(http%3A%2F%2Flocalhost%3A46223%2FScripts%2Fangular.min.js%3A17%3A381)
angular.js:38 未捕获的错误:[$injector:modulerr] http://errors.angularjs.org/1.3.12/ $injector/modulerr?p0=test&p1=Error%3A%20…0d%20(http%3A %2F%2Flocalhost%3A46223%2FScripts%2Fangular.min.js%3A17%3A381)
If I include all scripts in the html, before the angularjs file, it works, but I don't want that :/如果我在 html 中包含所有脚本,则在 angularjs 文件之前,它可以工作,但我不希望那样:/
Any solution ?任何解决方案?
Thanks for you answers !!谢谢你的回答!!
have you tried to do manual bootstrapping and run your scripts before angular.bootstrap as the following?您是否尝试过手动引导并在 angular.bootstrap 之前运行您的脚本,如下所示?
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']);
Note: remove ng-app from your DOM when doing manual bootstrapping注意:在进行手动引导时从 DOM 中删除 ng-app
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.