1

I am trying to show the filenames from an array into UI. Below is my Angular code:

 <div *ngFor="let file of myFiles">
      **{{ file.name }}**
 </div>

The code for AppComponent is:

export class AppComponent {
  public myFiles: Array<{ [key: string]: string | number }> = [
    { name: "First.txt", size: 500 },
    { name: "Second.jpg", size: 100 }
  ];

  public clearModel(): void {
    this.myFiles = [];
  }
}

I want to display the name parameter from my Array on the list. I get the error:"Property 'name' comes from an index signature, so it must be accessed with ['name'].ngtsc(4111)"

I have tried {{file[name]}} which returns error: Property 'name' does not exist on type 'AppComponent'

1
  • You should check your compiler options - There is nothing wrong with your code. Commented Aug 11, 2022 at 9:31

1 Answer 1

1

You should pass a string: file['name'].

Or you can disable this check in tsconfig.json:

"compilerOptions": {
  "noPropertyAccessFromIndexSignature": false
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great. It worked. That file['name'] is new to me. I thought I need to pass it like file[name] which failed. Thanks, Eugene.

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.