AndreaNobili
AndreaNobili

Reputation: 42957

Why am I obtaining undefined trying to retrieve an array from this class?

I am pretty new in Angular and TypeScript (I came from Java) and I have the following problem.

I have defined this class:

export class OrderFormValues {

  public statoOrdine: [
    {label:'Seleziona stato ordine', value:null},
    {label:'Aperto', value:'Aperto'},
    {label:'Chiuso', value:'Chiuso'},
  ]
}

that will contains some arrays that I will use to valorize input dropdown in my front end.

Then into my component class I am doing something like this:

ngOnInit() {

    console.log("orderFormValues VALUES: " + this.orderFormValues.statoOrdine);
    ........................................................
    ........................................................
    ........................................................
}

I expected to retrieve the array with its values but I am obtaining this output:

orderFormValues VALUES: undefined

Why it is undefined? What is wrong? What am I missing? How can I fix it?

Upvotes: 1

Views: 46

Answers (1)

Mikel
Mikel

Reputation: 234

the assignment of your array is incorrect as you are using : to assign values. When you use the two dots (:) it's meant to declare the type. myNumber: number = 5. So in your case, you'd have to do something like `

public statoOrdine: any = [...]

Any is not the most correct way to declare the type, but it will work.

Upvotes: 2

Related Questions