循环语句的Angular 2 typescript 1.8 compliation error

时间:2016-06-01 08:53:48

标签: loops typescript angular

我想循环播放一个数组。

有我的代码:

@Input()
applicationLinks: ApplicationLink[];


applicationRows: ApplicationLinkRow[];
for(applicationLink in applicationLinks) {
    applicationRows.push(applicationLink);
}

in给出编译错误

  

(=预期)。

我也试过

for(applicationLink of applicationLinks)
for(var applicationLink in applicationLinks)
for(var applicationLink of applicationLinks)

每次都会出现编译错误!

1 个答案:

答案 0 :(得分:3)

您需要将此代码移动到方法或构造函数

for(applicationLink in applicationLinks) {
    applicationRows.push(applicationLink);
}

应该是这样的:

class SomeClass {
  @Input()
  applicationLinks: ApplicationLink[];


  applicationRows: ApplicationLinkRow[];

  someMethod() {
    for(applicationLink in applicationLinks) {
        applicationRows.push(applicationLink);
    }
  }
}