Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 595
Tampa Avatar asked Jun 11 '16 07:06

Tampa


People also ask

How do you initialize an array of objects in TypeScript?

To declare an array of objects in TypeScript, set the type of the variable to {}[] , e.g. const arr: { name: string; age: number }[] = [] . Once the type is set, the array can only contain objects that conform to the specified type, otherwise the type checker throws an error. Copied!

How do you declare an array in TypeScript?

Arrays can be declared and initialized separately. let fruits: Array<string>; fruits = ['Apple', 'Orange', 'Banana']; let ids: Array<number>; ids = [23, 34, 100, 124, 44]; An array in TypeScript can contain elements of different data types using a generic array type syntax, as shown below.

How we can declare array in angular?

Datatype: array_name:datatype[]=[]; Example string: users:string[]=[]; For array of objects: Objecttype: object_name:objecttype[]=[{}]; Example user: Users:user[]=[{}]; And if in some cases it's coming undefined in binding, make sure to initialize it on Oninit() .

How do I declare a string array in angular 9?

How to declare and Initialize an array of objects with values. Objects are properties that contain keys and values, There is no type to represent the object in Typescript and angular. An array of strings can be declared and initialized with the below syntax. private arrays:Array<string> = ['one','two','three'];


4 Answers

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];
    }
}
like image 54
Temitope Fatayo Avatar answered Sep 28 '22 06:09

Temitope Fatayo


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

hero:Hero[]=[];
like image 27
Mahesh Yadav Avatar answered Sep 28 '22 05:09

Mahesh Yadav


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

like image 21
eko Avatar answered Sep 28 '22 06:09

eko


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

like image 28
Priyesh Kumar Avatar answered Sep 28 '22 06:09

Priyesh Kumar