I am using typescript to code in Angular2. I have this object:
export class Car{
name: String;
door: {
position: String;
id: Number;
};
}
I have initialized the object following this steps:
constructor() {
this.door= new Door();
}
export class Door{
position: String;
ID: Number
}
and it perfectly works. My problem begins when I try to initialize an array of objects
export class Car{
name: String;
door: {
position: String;
id: Number;
};
color: {
one: String;
two: String;
}[]
}
and I try to do the same Edited
constructor() {
for (var i = 0; i < 10; i++) {
this.color.push(new Color);
}
this.door= new Door();
}
export class Color{
one: String;
two: String;
}
The error is the following:
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'push' of undefined TypeError: Cannot read property 'push' of undefined
Colorobject to the array, you can do:this.color.push(new Color());. Note:colormay not be the ideal name for an array. :-){ one: String; two: String; }[]is an array