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

前端 未结 3 2100
时光取名叫无心
时光取名叫无心 2020-12-14 16:37

In my AngularJS app, I\'ve many controllers js files.

These controllers (one.ctrl.js,two.ctrl.js,...) needs to be included in

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-14 17:24

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

提交回复
热议问题