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?
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>
<% } %>
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
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With