When working in TypeScript, I have discovered a way to retrieve the content of a text file from my assets directory using the following code:
this.http.get('assets/data/file_1.txt')
.subscribe(data=>{
let obj = data['_body'];
console.log("obj "+JSON.stringify(obj)); // This log displays the content of the file
});
Is it possible to include a parameter or value in this request?
For example, if I have multiple files (file_4.txt, file_5.txt, file_1.txt) and I want to loop through each of them, dynamically changing the file index. My current implementation is as follows:
var fileIndex=[4,5,1];
for (i = 0; i < this.fileIndex.length; i++) {
this.http.get('assets/data/file_'+this.fileIndex[i]+'.txt')
.subscribe(data=>{
let obj = data['_body'];
console.log("obj "+JSON.stringify(obj));
console.log("This is file number "+this.fileIndex[i]); // issue arises here
});
}
Unfortunately, the file number cannot be logged for each request due to the asynchronous nature of the get request. The variable 'this.fileIndex[i]' returns "undefined" within the subscribe block. Is there a way to pass a parameter/value into the request to access it inside the subscribe function? Alternatively, how can I access the value of 'this.fileIndex[i]' within the subscribe function?