简体   繁体   English

如何向array- Angular2添加值

[英]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. 我在angular2中制作了这样的数组,并且我还制作了一个表格来填写表格并且效果很好。 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}>();

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"); 
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM