5

Is there any function to list all file names from a folder using angular js. Since I'm having plenty if .js file to include. So is there any function for the same?

3
  • Use Directory.GetFiles(path) method to list all the files and then in angular use ng-repeat and build a URL on the client Commented Apr 24, 2016 at 4:34
  • 2
    You can't list directories with angularjs. You can do that with nodejs for instance and then pass the result to angularjs for further action. Commented Apr 24, 2016 at 4:36
  • Another approach could be create a .json file which contains all your .js file names, then read that json using jquery getJSON method and include them in your angularjs app. Commented Apr 24, 2016 at 4:42

3 Answers 3

2

I think you can do this in nodejs. My requirement, was to list the files as a source for the APP in the index.html. Please see if this is helpful.

//// THIS WORKS FOR ME 
///// in app.js or server.js

var app = express();

app.use("/", express.static(__dirname));
var fs = require("fs"),

function getFiles (dir, files_){
    files_ = files_ || [];
    var files = fs.readdirSync(dir);
    for (var i in files){
        var name = dir + '/' + files[i];
        if (fs.statSync(name).isDirectory()){
            getFiles(name, files_);
        } else {
            files_.push(name);
        }
    }
    return files_;
}
//// send the files in js folder as variable/array 
ejs = require('ejs');

res.render('index', {
    'something':'something'...........
    jsfiles: jsfiles,
});

///--------------------------------------------------

I did Something like this to list the files in my index file in views/index.ejs --- the below code will list the files in index.ejs

<% for(var i=0; i < jsfiles.length; i++) { %>
   <script src="<%= jsfiles[i] %>"></script>
<% } %>
Sign up to request clarification or add additional context in comments.

Comments

1

If you are in a web environment your application is running client side into a browser then you cannot directly access to client or server filesystem.

In this case you need a descriptor like a php, asp.net page for generate a json file containing your file list.

PHP SIDE:

<?php
$dir = "FOLDER NAME HERE";
$return_array = array();
if(is_dir($dir)){
    if($dh = opendir($dir)){
        while(($file = readdir($dh)) != false){
            if($file == "." or $file == ".."){
            } else {
                $return_array[] = $file;
            }
        }
    }
    echo json_encode($return_array);
}
?>

ANGULAR SIDE

$http({
  method: 'GET',
  url: 'YOUR PHP PAGE URL HERE'
}).then(function successCallback(response) {

    // RESPONSE CONTAINS YOUR FILE LIST

  }, function errorCallback(response) {

    // ERROR CASE

  });

Comments

0

Thanks for your comments. I think its better to use require.js.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.