Search code examples
angularangular2-components

Angular 2 declaring dynamic property inside component


I want to declare some properties inside the component like so

export class HeroComponent implements OnInit {

    hero1:
    hero2:
    hero3:
    hero4:
    ...
    hero999:

}

Is there a better way to declare these properties instead of writing them all out?


Solution

  • export class HeroComponent implements OnInit {
      heroes: any[] = [];
    
      constructor() {
        for(var i = 1; i < 1000; i++) {
          this.heroes.push('hero'+i);
        }
      }
    }