Hi
In below code i am getting error
Code
TypeScript
uploadImage(event:any){
debugger;
const file = event.currentTarget.files[0];
if(event.target.files.length > 0 ){
if(file.type == 'image/png' || file.type == 'image/jpeg') {
const formObj = new FormData();
formObj.append('file',file);
this.post('storeapi.gerasim.in/Customer/Upload',formObj).subscribe((res: any)=>{
this.uploadedFileNames.push(res);
});
}
}
}Thanks

Jayraj ChhayaRank #304 · 8.2K reputationPosted May 30, 2024, 1:51 AM EDT
Problem
The code has a bug that causes an error: "Property file does not exist on type 'Advance Component'". This error occurs because the variable
fileis not declared within the scope of the loop in the image display section.Reason
The issue arises from attempting to access the
filevariable within the loop@for (item of uploadedFileNames). Thefilevariable is defined within theuploadImagefunction but is being referenced outside its scope, leading to a TypeScript compilation error.Solution
To fix the bug and resolve the TypeScript error, we need to ensure that the
filevariable is accessible within the loop. One way to achieve this is by modifying the code structure to include thefilevariable in the loop's scope.By using the
*ngFordirective in Angular templates, we can iterate over theuploadedFileNamesarray and access thefilevariable within the loop's scope, resolving the TypeScript error.Ramco RamcoRank #462 · 5.4K reputationPosted May 30, 2024, 2:05 AM EDT
Hi Jayraj
If i want to use @for (item of uploadedFileNames) then what i need to change
Thanks
Jayraj ChhayaRank #304 · 8.2K reputationPosted May 30, 2024, 2:53 AM EDT
Hi Ramco,
If you are using Angular 17, then
*ngFordirective has been replaced with the@fordirective for iterating over arrays.Angular 17