初始化类的数组:Angular 5

时间:2018-07-12 22:37:57

标签: angular typescript

我正在尝试以角度5初始化类的数组。

export class B{

  public Colors: string;
  public Temp: number;
  public Numbers: number;

}

export class A{

  public MyTable: B[];
  public othervar: number;
  ...
  ...
}

然后我尝试像这样初始化MyTable:

var test = new A();

test.MyTable[0].Numbers = 25;
test.MyTable[0].Colors= "blue";

这是正确的吗?

1 个答案:

答案 0 :(得分:1)

您还有new B()的新用法:

const test = new A();

test.MyTable = [];
test.MyTable[0] = new B();
test.MyTable[0].Numbers = 25;
test.MyTable[0].Colors= "blue";