Tampa
Tampa

Reputation: 78254

How to initialize an array in angular2 and typescript

Why does this happen in Angular2 and Typescript?

export class Environment {
    constructor(
      id: string,
      name: string
    ) { }
}


 environments = new Environment('a','b');



app/environments/environment-form.component.ts(16,19): error TS2346: Supplied parameters do not match any signature of call target.

How on do I initialize an array?

Upvotes: 26

Views: 186147

Answers (6)

Priyesh Kumar
Priyesh Kumar

Reputation: 2857

I don't fully understand what you really mean by initializing an array?

Here's an example:

class Environment {

    // you can declare private, public and protected variables in constructor signature 
    constructor(
        private id: string,
        private name: string
    ) { 
        alert( this.id );
    }
}


let environments = new Environment('a','b');

// creating and initializing array of Environment objects
let envArr: Array<Environment> = [ 
        new Environment('c','v'), 
        new Environment('c','v'), 
        new Environment('g','g'), 
        new Environment('3','e') 
  ];

Try it here : https://www.typescriptlang.org/play/index.html

Upvotes: 4

Mahesh Yadav
Mahesh Yadav

Reputation: 388

hi @JackSlayer94 please find the below example to understand how to make an array of size 5.

class Hero {
    name: string;
    constructor(text: string) {
        this.name = text;
    }

    display() {
        return "Hello, " + this.name;
    }

}

let heros:Hero[] = new Array(5);
for (let i = 0; i < 5; i++){
    heros[i] = new Hero("Name: " + i);
}

for (let i = 0; i < 5; i++){
    console.log(heros[i].display());
}

Upvotes: 0

Mahesh Yadav
Mahesh Yadav

Reputation: 388

you can create and initialize array of any object like this.

hero:Hero[]=[];

Upvotes: 24

Temitope Fatayo
Temitope Fatayo

Reputation: 419

You can use this construct:

export class AppComponent {

    title:string;
    myHero:string;
    heroes: any[];

    constructor() {
       this.title = 'Tour of Heros';
       this.heroes=['Windstorm','Bombasto','Magneta','Tornado']
       this.myHero = this.heroes[0];
    }
}

Upvotes: 40

user6453173
user6453173

Reputation:

In order to make more concise you can declare constructor parameters as public which automatically create properties with same names and these properties are available via this:

export class Environment {

  constructor(public id:number, public name:string) {}

  getProperties() {
    return `${this.id} : ${this.name}`;
  }
}

let serverEnv = new Environment(80, 'port');
console.log(serverEnv);

 ---result---
// Environment { id: 80, name: 'port' }

Upvotes: 0

eko
eko

Reputation: 40647

Class definitions should be like :

export class Environment {
    cId:string;
    cName:string;

    constructor( id: string, name: string ) { 
        this.cId = id;
        this.cName = name;
    }

    getMyFields(){
        return this.cId + " " + this.cName;
    }
}

 var environments = new Environment('a','b');
 console.log(environments.getMyFields()); // will print a b

Source: https://www.typescriptlang.org/docs/handbook/classes.html

Upvotes: 10

Related Questions