Wesley Coetzee
Wesley Coetzee

Reputation: 4838

Creating an array of a class Angular 2

I need to create an array of a class using Angular 2 and TypeScript.

import { ClassX } from '...';    

public ListX: ClassX[];

Once I have the list, I want to then add more empty instances of ClassX

action() {
  this.ListX.push(ClassX);
}

This would then add a new ClassX instance to the front end

Upvotes: 1

Views: 14121

Answers (1)

Maximilian Riegler
Maximilian Riegler

Reputation: 23506

I'm not entirely sure what you're trying to achieve, but adding a new instance of something usually includes using the keyword new like this:

this.ListX.push(new ClassX());

And instantiation of an array is done like so:

public ListX: ClassX[] = [];

or:

public ListX: Array<ClassX> = new Array<ClassX>();

While I prefer the latter, since it's more clear, what is happening.

Upvotes: 5

Related Questions