arraysangulartypescriptclassempty-list

problem in defining empty list of a class in angular 12


i have a problem defining a empty list of a class in angular.this is my class

export class category{
public Id:number;
public Name:string;
constructor(
){}}

enter image description here

i got this error.

An element access expression should take an argument

any help will be highly appreciated


Solution

  • You got this error because you are trying to declare your variable after import statements, not into the class.
    You should declare it as below:

    //import statements
    @Component({
      selector: "app-root",
      templateUrl: "./app.component.html",
      styleUrls: ["./app.component.css"],
    })
    export class AppComponent {
      category: category[] = [];
      constructor() {}
    
      ngOnInit() {}
    }