Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add values to array- Angular2

I made an array like this in angular2, and also I made a form to fill out a table and it works well. But my question is what is the best way to put some values in this array without filling the form, I want some content already in this array.

employeeList = new Array<{name:string, bio:string, job:string, salery:string, url:string}>();
like image 280
Aleksandar Milicevic Avatar asked Jun 14 '16 22:06

Aleksandar Milicevic


1 Answers

Create a class representing your structure:

export class User {
  name: string;
  bio: string;
  job: string;
  salary: string;
  url: string
  constructor(_name: string, _bio: string, _job: string, _salary: string, _url: string) {
    this.name = _name; this.bio = _bio; this.job = _job; this.salary = _salary; this.url = _url;
  }
}

or like this:

export class User {
  constructor(public name: string,
    public bio: string,
    public job: string,
    public salary: string,
    public url: string) {
  }
}

You array will look like this:

users: User[] = []; // if it's a class member
var users: User[] = []; // if it's a local variable

Add something to array:

this.users.push(
   new User("Bob", "", "Developer", "100", "github.com"); 
)
like image 166
Andrei Zhytkevich Avatar answered Oct 27 '22 22:10

Andrei Zhytkevich