typescript - Jak přidat hodnoty do pole - Angular2
V Angular2 jsem vytvořil pole takto a také jsem vytvořil formulář pro vyplnění tabulky a funguje to dobře. Ale moje otázka je, jaký je nejlepší způsob, jak vložit do tohoto pole nějaké hodnoty bez vyplňování formuláře, chci mít v tomto poli už nějaký obsah.
employeeList = new Array<{name:string, bio:string, job:string, salery:string, url:string}>();
Odpověď č. 1
Vytvořte třídu představující vaši strukturu:
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;
}
}
nebo takto:
export class User {
constructor(public name: string,
public bio: string,
public job: string,
public salary: string,
public url: string) {
}
}
Váš pole bude vypadat takto:
users: User[] = []; // if it's a class member
var users: User[] = []; // if it's a local variable
Přidat něco do pole:
this.users.push(
new User("Bob", "", "Developer", "100", "github.com");
)
