最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parameter declaration expected (typescript) - Stack Overflow

programmeradmin1浏览0评论

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

@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);
      }
   }
 }
Share Improve this question edited May 23, 2017 at 12:16 CommunityBot 11 silver badge asked Jan 24, 2017 at 19:00 lowcrawlerlowcrawler 7,60910 gold badges58 silver badges95 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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);
        }
    }
  }
}
发布评论

评论列表(0)

  1. 暂无评论