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);
      }
   }
 }

1 Answer 1

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);
        }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.