1

I am able to get data from the REST API in my application, i am able to print the data on to the console, but no idea how do i display on to html, can any one help me on this please?

App.component.ts

import { HttpClient } from '@angular/common/http';
import { Component, Inject } from '@angular/core';
import { Employee } from './employee';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'emp-data-example';
  employees:Employee[]=[];
  constructor(@Inject(HttpClient) private http:HttpClient){

  }

  ngOnInit(){
    this.http.get("http://localhost:8080/api/all").subscribe(
    response=>{
      this.employees=response; // here is error because response is an Object and employees is an array
    });
    //resp.subscribe((data)=>{this.employees=data});
    
  }

}

app.component.html

<div>
  <table border="1">
    <tr>
      <th>Emp Id</th>
      <th>Emp Name</th>
      <th>Salary</th>
    </tr>
    <tr ngFor="let employee of employees">
      <td>{{employee.id}}</td>
      <td>{{employee.name}}</td>
      <td>{{employee.department}}</td>
      <td>{{employee.salary}}</td>
    </tr>
  </table>
</div>
6
  • try this this.employees=[response]; Commented Dec 3, 2020 at 5:51
  • No its saying,The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? Type 'Object' is missing the following properties from type 'Employee': id, name, department, salaryts(2696) Commented Dec 3, 2020 at 5:53
  • so can you show your response object? Commented Dec 3, 2020 at 5:53
  • [{"id":0,"name":"sudheer","department":"IT","salary":12344.0},{"id":23,"name":"kumar","department":"ECE","salary":12345.0}] is the response i am getting from REST and same only printing on console. Commented Dec 3, 2020 at 6:09
  • if this is the response, your code should work. But make sure your Employee model has the properties Commented Dec 3, 2020 at 6:11

2 Answers 2

3

Try to set the type of the response to Employee and add it to your array like so:

    this.http.get("http://localhost:8080/api/all").subscribe(
    (response: Employee[]) =>{
      this.employees.push(...response);
    });
Sign up to request clarification or add additional context in comments.

5 Comments

I have added the above code, but in console its saying ERROR TypeError: Cannot read property 'id' of undefined. can you please tell me whats wrong with app.component.html ?
this is not compatible with the response he described . the response is an array of employees as is the employees object. here you are trying to push an array into array.
then: (response: Employee[]) =>{ this.employees.push(...response);
@misha1109 this would work, but it's not that most elegant way.
@vladkatz seems almost identical your answer, i prefer to push to the array in case the http request will not only happen onInit
2

since you are getting array in the response and its from an employee type it should be something like that:

this.http.get("http://localhost:8080/api/all").subscribe(
(response: Array<Employee>) =>{
  this.employees = response;
});

2 Comments

Thank you very much :) I really appreciate your work.
any time. this should work event without the casting, but you should always check the combability between the receiving object from the API and the assigned object inside the component/service. if you'll try putting an array inside an regular object, you'll get errors and vise versa

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.