如何将值添加到数组-Angular2 [英] How to add values to array- Angular2

查看:109
本文介绍了如何将值添加到数组-Angular2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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}>();

推荐答案

创建一个代表您的结构的类:

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

或类似这样:

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

这篇关于如何将值添加到数组-Angular2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆