I'm using Ionic 2, which sits on top of Angular 2. I need to create an array of items. I don't know how many items are going to be in that array.
Here is my Typescript, simplified:
import { Component } from '@angular/core';
import { VgAPI } from 'videogular2/core';
@Component({
selector: 'page-class',
templateUrl: 'class.html'
})
export class ClassPage {
api: any[];
constructor(...){}
// Load API when videos are ready
onPlayerReady(api: VgAPI, i: any) {
this.api[i] = api;
}
}
onPlayerReady is called when video players in my view intialize. i is the ID of that player (0, 1, 2, ...).
I'd expect this to construct an array of:
this.api[0] = VgAPI (of player 1)
this.api[1] = VgAPI (of player 2)
this.api[2] = VgAPI (of player 3)
this.api[3] = VgAPI (of player 4)
this.api[4] = VgAPI (of player 5)
Unfortunately, I get the following:
Runtime Error
Cannot set property '1' of undefined
I believe this is because this.api[0] isn't explicitly defined anywhere. But that's a problem, as I don't know how many items where will be. How can I properly define this array to allow this behaviour?