ArunJaganathan
ArunJaganathan

Reputation: 713

How to list all file name from a folder using angular js

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?

Upvotes: 5

Views: 13636

Answers (3)

ArunJaganathan
ArunJaganathan

Reputation: 713

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

Upvotes: 0

SuperNova
SuperNova

Reputation: 27456

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>
<% } %>

Upvotes: 2

danilonet
danilonet

Reputation: 1795

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

  });

Upvotes: 1

Related Questions