将JSON数据推入对象数组ANGULAR

时间:2019-12-08 16:52:20

标签: arrays json angular object angular6

private contacts: Array < Contact > = [{
  first_name: "Darko",
  last_name: "Lesevic",
  email: ["darko@test.com", "najjaci@test.com"],
  phone: [123456789, 123456789, 123456789],
  photo: "",
  id: 1
}, ];


getContactsJSON(): Observable < IContact[] > {
  return this.http.get < IContact[] > ("/assets/data/contacts.json");
};
//I want to combine these two 
getAllContacts(): Observable < IContact[] > {
  return of(this.contacts);
};

我想将JSON数据放入通讯录数组,因为我正在制作CRUD应用程序,但不知道如何,请帮助我。 Here is my photohere is my JSON data and class。我想保留JSON和我自己的类中的数据吗?

2 个答案:

答案 0 :(得分:0)

可以订阅您的API请求并将结果应用于您的数组:

this.getContactsJSON.subscribe(s=> {
    this.contacts = s;
    console.log(this.contacts); // you can check your array `this.contacts`
})

答案 1 :(得分:0)

contacts: Contact[] = JSON.parse(jsonString);