Как сохранить несколько объектов JSON в Angular7 - PullRequest
0 голосов
/ 16 мая 2019

Как сохранить несколько объектов json в массиве angular7?

Я пытаюсь сохранить массив объектов json, как показано ниже:

employees: any;

 ngOnInit() {
    this.getemployee().subscribe 
    data => this.employees == data,
    );
}

ниже приведен ответ, который я получаюиз бэкэнда:

(32) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {id: "59744", employee_name: "Sample API ", employee_salary: "18000", employee_age: "20", profile_image: ""}
1: {id: "59747", employee_name: "Test", employee_salary: "123", employee_age: "456", profile_image: ""}
2: {id: "59748", employee_name: "hello#amdon", employee_salary: "10000", employee_age: "35", profile_image: ""}
.......

Ответы [ 2 ]

1 голос
/ 16 мая 2019

создать массив объектов следующей структуры.

  //response  data structure
   export class ResponseData {
     id:string
     employee_name: string;
     employee_salary: string; 
     employee_age: string;
     profile_image:string;
   }

component.ts

export class Test implements OnInit { 

 responseData:ResponseData[]=[]; 

 constructor(){ }

 ngOnInit() {
    this.getemployee().subscribe((data)=>{
       if(data != null) {
       console.log('response received');
       this.responseData = data;
       }
    });   
  }
}
0 голосов
/ 16 мая 2019

Попробуйте это решение:

 Component.ts:

     employees: IEmployeeData[] = []; 

     constructor(){ }

     ngOnInit() {
        this.getemployee().subscribe((data: IEmployeeData) => {
           if(data) this.employees = data;
        });   
     }

   interface IEmployeeData { 
     id:string
     employee_name: string;
     employee_salary: string; 
     employee_age: string;
     profile_image:string;
   };
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...