简体   繁体   English

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

[英]Include all javascript files in a directory to a html file in angularjs? with grunt?

In my AngularJS app, I've many controllers js files. 在我的AngularJS应用程序中,我有很多控制器js文件。

These controllers ( one.ctrl.js , two.ctrl.js , ... ) needs to be included in my index.html file. 这些控制器( one.ctrl.jstwo.ctrl.js... )需要包含在我的index.html文件中。

Directory Structure: 目录结构:

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

Currently, above js files are included in index.html file as follows. 目前,上面的js文件包含在index.html文件中,如下所示。

index.html: index.html的:

<!--   other html components   -->

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

There are gonna be more *.ctrl.js files which are required to be included in index.html . 将有更多的*.ctrl.js文件需要包含在index.html

I need a way to automatically include all the *.ctrl.js files in controllers directory to index.html . 我需要一种方法来自动将controllers目录中的所有*.ctrl.js文件包含到index.html

Findings: 发现:

From some SO questions, 从一些SO问题,

Load JavaScript and CSS files in folders in AngularJS 在AngularJS中的文件夹中加载JavaScript和CSS文件

How can I include all JavaScript files in a directory via JavaScript file? 如何通过JavaScript文件将所有JavaScript文件包含在目录中?

I found that it cannot be done automatically and needs some server side scripting or build tools. 我发现它不能自动完成,需要一些服务器端脚本或构建工具。

My Question: 我的问题:

Currently, I'm using yeoman which include grunt for build tool. 目前,我正在使用包含用于构建工具的grunt yeoman So, my question is, How can those javascript files in a directory be automatically included in a html file? 所以,我的问题是,目录中的那些javascript文件如何自动包含在html文件中?

You could use the grunt-include-source plugin 您可以使用grunt-include-source插件

Using it you can define templates like these : 使用它,您可以定义以下模板:

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

in your html file which will be expanded to include all your source js and css files present in your source location which can be configured in the grunt task 在你的html文件中,它将被扩展为包含源位置中存在的所有源js和css文件,可以在grunt任务中配置

Answering the general question of adding source files to index.html on demand and automatically and elaborating on the use of grunt-include-source . 回答了根据需要向index.html添加源文件的一般问题,并自动详细说明了grunt-include-source的使用

This is for the following folder structure: 这适用于以下文件夹结构:

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

  2. In your Gruntfile.js , add grunt.loadNpmTasks('grunt-include-source'); 在你的Gruntfile.js ,添加grunt.loadNpmTasks('grunt-include-source');

  3. Then you have to add a bunch of tasks to your Gruntfile.js , after which it should look like this: 然后你必须向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. In your index.html , add this ( the file path here looks inside the base path set in Gruntfile.js ): index.html ,添加它( 此处的文件路径在Gruntfile.js设置的基本路径内部 ):

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

     grunt 

This should start off all tasks in sequence, and your index.html should be updated accordingly when a JS or CSS is added or removed. 这应该按顺序启动所有任务,并且在添加或删除JS或CSS时应相应地更新index.html。

This is how your index.html might look like with a small number of files: 这就是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 -->

Links: 链接:

You can use something like this: 你可以使用这样的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM