1

I am trying to initialize an array of a class in angular 5.

export class B{

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

}

export class A{

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

Then I try to initialize MyTable like that:

var test = new A();

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

Is this correct ?

1

1 Answer 1

1

You also have new use new B():

const test = new A();

test.MyTable = [];
test.MyTable[0] = new B();
test.MyTable[0].Numbers = 25;
test.MyTable[0].Colors= "blue";
Sign up to request clarification or add additional context in comments.

7 Comments

I did that, but then my whole html component is messed up. When ever I try to play with that array nothing in my html component is being displayed. I have interpolations in there and none of them works as soon as I do anything with test.MyTable array...
Can you post that HTML? There's no reason adding proper initialization of a class instance should, in and of itself, mess anything up.
I added a line to also initialize the array.
Here a live test of my issue: stackblitz.com/edit/…
You have <table> markup around something which isn't a table -- that's just going to be invisible. I changed it to a <div> just to see what was going on. <div> Of the markup inside that div, only {{_types.MyTable[0].Colors }} made sense for your class -- the rest was causing errors.
|

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.