在angular.module之前加载javascript文件 [英] Load javascript files before angular.module

查看:61
本文介绍了在angular.module之前加载javascript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create a sort of js library. The user should just include ONE js file and he can use all my functions.

我正在使用angularjs.

I'm using angularjs.

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

In my angular.module, I have several "Injectors" (correct me if it's not the name) define as follow :

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

要使其正常工作,我需要在angular js文件之前包含一些JS文件(例如"ng-currency.js").但是,我希望用户仅包含此angularjs文件(或仅包含一个,不再更多)...

To make it working, I need to include some JS files (like "ng-currency.js") before the angular js file. BUT, I want the user has just to include this angularjs file (or just ONE other, no more)...

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

I tried to create a new JS file to load everything before my angularjs file, like that :

$.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 = test& p1 =错误%3A%20…0d%20(http%3A%2F%2Flocalhost%3A46223%2FScripts%2Fangular.min.js%3A17%3A381)

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)

如果我将所有脚本都包含在html中,则在angularjs文件之前,它可以工作,但我不希望这样:/

If I include all scripts in the html, before the angularjs file, it works, but I don't want that :/

任何解决方案吗?

谢谢您的回答!

推荐答案

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

have you tried to do manual bootstrapping and run your scripts before angular.bootstrap as the following?

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-app

Note: remove ng-app from your DOM when doing manual bootstrapping

这篇关于在angular.module之前加载javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆