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

是否将目录中的所有javascript文件包含到angularjs中的html文件中?咕噜声?,javascript,angularjs,gruntjs,Javascript,Angularjs,Gruntjs,在我的AngularJS应用程序中,我有许多控制器js文件 这些控制器(one.ctrl.js,two.ctrl.js,…)需要包含在我的index.html文件中 目录结构: app/ js/ controllers/ one.ctrl.js two.ctrl.js <!-- other html components --> <script src="js/controllers/one.ctrl.js"/&

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

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

目录结构:

app/
   js/
      controllers/
         one.ctrl.js
         two.ctrl.js
<!--   other html components   -->

<script src="js/controllers/one.ctrl.js"/>
<script src="js/controllers/two.ctrl.js"/>
</body>
</html>
目前,上述
js
文件包含在
index.html
文件中,如下所示

index.html:

app/
   js/
      controllers/
         one.ctrl.js
         two.ctrl.js
<!--   other html components   -->

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

还有更多的
*.ctrl.js
文件需要包含在
index.html

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

调查结果:

app/
   js/
      controllers/
         one.ctrl.js
         two.ctrl.js
<!--   other html components   -->

<script src="js/controllers/one.ctrl.js"/>
<script src="js/controllers/two.ctrl.js"/>
</body>
</html>
从一些所谓的问题来看

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

我的问题:

app/
   js/
      controllers/
         one.ctrl.js
         two.ctrl.js
<!--   other html components   -->

<script src="js/controllers/one.ctrl.js"/>
<script src="js/controllers/two.ctrl.js"/>
</body>
</html>
目前,我正在使用
yeoman
,其中包括
grunt
作为构建工具。 所以,我的问题是,如何将目录中的javascript文件自动包含在html文件中?

您可以使用该插件

使用它,您可以定义如下模板:

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

在html文件中,该文件将被扩展以包括源位置中的所有源js和css文件,这些文件可以在grunt任务中配置

(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;
 }
})();

回答了按需自动向index.html添加源文件的一般问题,并详细介绍了

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

MyProject
|
+-- index.js
+-- Gruntfile.js
+-- package.json
+-- //other files
|
+--+ app
   +-- //app files (.html,.js,.css,.*)
  • 使用npm i-D grunt安装,包括源grunt contrib watch

  • grunfile.js
    中,添加
    grunt.loadNpmTasks('grunt-include-source')

  • 然后,您必须向
    grunfile.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']);
    };
    
  • index.html
    中,添加以下内容(此处的文件路径位于
    grunfile.js
    中设置的基本路径内):

  • 这应该按顺序启动所有任务,并且当添加或删除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 -->
    
    
    
    链接:


    我可以用gulp.js给你举个例子。这样行吗?我还没有用过
    gulp.js
    。如果可以的话我可以试试。你能提供一个完整的Gruntfile.js吗?