1

@Vinay in this TypeScript + AngularJS 1: How to connect enum with select directive? question shows a relatively simple way to get a array for building a select drop-down in angular.

Unfortunately, I try to ape this code and I get errors ... first upon declaring the 'colors' array if I use var or let... (but it works if I don't). Unfortunately, that just moves the error to the next variable declaration in the setup of the for loop. Unfortunately, here, I can't not put in a let or a var.

I'm sure this is simple, but I'm just banging me head and missing it.

enum Color {
    Green = <any>"Green",
    Red = <any>"Red",
    Blue = <any>"Blue"
  }

export class ClassName {
  colors: string[] = [];  // <-- get error here if I declare var or let
  for (var item in Color) {  // <-- get error here
      if (Color.hasOwnProperty(item)) {
          this.colors.push(item);
      }
   }
 }
Community
  • 1
  • 1
lowcrawler
  • 6,777
  • 9
  • 37
  • 79

1 Answers1

3

Property declarations belong in the body, but executable code goes in the constructor:

export class ClassName {
  colors: string[] = [];  // <-- get error here if I declare var or let
  constructor() {
    for (var item in Color) {  // <-- get error here
        if (Color.hasOwnProperty(item)) {
            this.colors.push(item);
        }
    }
  }
}
Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235