如何向array- Angular2添加值

时间:2016-06-14 22:21:35

标签: arrays typescript

我在angular2中创建了一个这样的数组,并且我还创建了一个表格来填写表格并且效果很好。但我的问题是在没有填写表单的情况下将一些值放入此数组的最佳方法是什么,我想在此数组中已有一些内容。

employeeList = new Array<{name:string, bio:string, job:string, salery:string, url:string}>();

1 个答案:

答案 0 :(得分:19)

创建一个表示结构的类:

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;
  }
}

或者像这样:

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

您的数组将如下所示:

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

向数组添加内容:

this.users.push(
   new User("Bob", "", "Developer", "100", "github.com"); 
)