将目录中的所有javascript文件包含到angularjs中的html文件中?咕噜咕噜?

时间:2014-06-07 12:23:31

标签: javascript angularjs gruntjs

在我的AngularJS应用程序中,我有很多控制器js文件。

这些控制器(one.ctrl.jstwo.ctrl.js...)需要包含在我的index.html文件中。

目录结构:

app/
   js/
      controllers/
         one.ctrl.js
         two.ctrl.js

目前,js文件中包含以上index.html个文件,如下所示。

的index.html:

<!--   other html components   -->

<script src="js/controllers/one.ctrl.js"/>
<script src="js/controllers/two.ctrl.js"/>
</body>
</html>

*.ctrl.js中需要包含更多index.html个文件。

我需要一种方法来自动将*.ctrl.js目录中的所有controllers个文件包含到index.html

调查结果:

从一些问题来看,

Load JavaScript and CSS files in folders in AngularJS

How can I include all JavaScript files in a directory via JavaScript file?

我发现它不能自动完成,需要一些服务器端脚本或构建工具。

我的问题:

目前,我正在使用yeoman,其中包含grunt用于构建工具。 所以,我的问题是,如何将目录中的javascript文件自动包含在html文件中?

3 个答案:

答案 0 :(得分:13)

您可以使用grunt-include-source插件

使用它可以定义以下模板:

html: {
    js: '<script src="{filePath}"></script>',
    css: '<link rel="stylesheet" type="text/css" href="{filePath}" />',
  }

在你的html文件中,它将被扩展为包含源位置中存在的所有源js和css文件,可以在grunt任务中配置

答案 1 :(得分:6)

回答将源文件按需添加到index.html的一般问题,并自动详细说明https://gist.github.com/prasann/9003350的使用。

这适用于以下文件夹结构:

MyProject
|
+-- index.js
+-- Gruntfile.js
+-- package.json
+-- //other files
|
+--+ app
   +-- //app files (.html,.js,.css,.*)
  1. 使用npm i -D grunt-include-source grunt-contrib-watch安装。

  2. Gruntfile.js中,添加grunt.loadNpmTasks('grunt-include-source');

  3. 然后你必须向你的Gruntfile.js添加一堆任务,之后它应该是这样的:

    module.exports = function (grunt) {
        grunt.initConfig({
            pkg: grunt.file.readJSON('package.json'),
            //...
            watch: {
                //...
                includeSource: {
                    // Watch for added and deleted scripts to update index.html
                    files: ['app/**/*.js','app/**/*.css'],
                    tasks: ['includeSource'],
                    options: {
                        event: ['added', 'deleted']
                    }
                }
            },
            includeSource: {
                options: {
                    //This is the directory inside which grunt-include-source will be looking for files
                    basePath: 'app'
                },
                app: {
                    files: {
                        //Overwriting index.html
                        'app/index.html': 'app/index.html'
                    }
                }
            }
        });
    
        //...
        grunt.loadNpmTasks('grunt-contrib-watch');
        grunt.loadNpmTasks('grunt-include-source');
    
        //...
        //Include sources, run the server, open the browser, and watch.
        grunt.registerTask('default', ['includeSource', 'watch']);
    };
    
  4. index.html中添加此内容(此处的文件路径查看Gruntfile.js 中设置的基本路径):

    <!-- include: "type": "css", "files": "**/*.css" -->
    <!-- /include -->
    <!-- include: "type": "js", "files": "**/*.js" -->
    <!-- /include -->
    
  5. 最后,在命令行上:

    grunt
    
  6. 这应该按顺序启动所有任务,并且在添加或删除JS或CSS时应相应地更新index.html。

    这是index.html使用少量文件时的样子:

    <!-- include: "type": "css", "files": "**/*.css" -->
    <link href="styles.css" rel="stylesheet" type="text/css">
    <!-- /include -->
    <!-- include: "type": "js", "files": "**/*.js" -->
    <script src="_routes/routes.js"></script>
    <script src="scripts/app.js"></script>
    <!-- /include -->
    

    链接:

答案 2 :(得分:1)

您可以使用以下内容:

(function loadScript() {
    var head = document.getElementsByTagName("head")[0];
    var done = false;

    var directory = 'libraries/';
    var extension = '.js';
    var files = ['functions', 'speak', 'commands', 'wsBrowser', 'main']; 

     for (var file of files){ 
        var path = directory + file + extension; 
        var script = document.createElement("script");
        script.src = path;

        script.onload = script.onreadystatechange = function() {
        // attach to both events for cross browser finish detection:
        if ( !done && (!this.readyState ||
           this.readyState == "loaded" || this.readyState == "complete") ) {
           done = true;
           // cleans up a little memory:
           script.onload = script.onreadystatechange = null;
           // to avoid douple loading
           head.removeChild(script);
        }
    };
  head.appendChild(script); 
  done = false;
 }
})();