1

Data fetched from api is not getting displayed on screen but i am getting the data in console .

data.service.ts

    const httpOptions = {
      headers:new HttpHeaders({'Content-Type':'application/json'})
     };

    @Injectable()

    export class DataService {

    constructor(private http: HttpClient) {}
    getuserdetails(){
    return this.http.get<any> 
    ('http://jsonplaceholder.typicode.com/users/1')
    .pipe(map(item => {
        return item;
    }));
    }
   }

information.component.ts

 export class InformationComponent implements OnInit {
    dataItem:any = {} 

    constructor(private data: DataService) {
    this.data.getuserdetails().subscribe(item=>{
    console.log(item);
    this.dataItem=item;
  });
   }

  ngOnInit() {
  }

 }

information.component.html

<div *ngIf="dataItem?.length>=0">           
 <div *ngFor='let x of dataItem'>

   {{x.name}}

 </div>
 </div>

Here i am getting the details of user 1 in console but its not getting displayed on main screen.

8
  • Can you show us your data (this.dataItem) Commented Apr 20, 2019 at 10:39
  • {id: 1, name: "Leanne Graham", username: "Bret", email: "[email protected]", address: {…}, …} address: {street: "Kulas Light", suite: "Apt. 556", city: "Gwenborough", zipcode: "92998-3874", geo: {…}} company: {name: "Romaguera-Crona", catchPhrase: "Multi-layered client-server neural-net", bs: "harness real-time e-markets"} email: "[email protected]" id: 1 name: "Leanne Graham" phone: "1-770-736-8031 x56442" username: "Bret" website: "hildegard.org" Commented Apr 20, 2019 at 10:42
  • Well if dataItem === that data, you cant access .length, since it will return undefined. That is not iterable for javascript Commented Apr 20, 2019 at 10:44
  • So instead of <div *ngIf="dataItem?.length>=0"> , what should be the condition Commented Apr 20, 2019 at 10:46
  • 1
    I would set default dataItem to null, then simply do ngIf="dataItem" Another thing, you are trying again to iterate in ngFor, there is no need for that, just access data using {{dataItem.name}} Commented Apr 20, 2019 at 10:48

2 Answers 2

2

Try putting code in ngOnInit

ngOnInit(){
 this.data.getuserdetails().subscribe(item=>{
  console.log(item);
  this.dataItem=item;
 });
}

and verify in html by doing

<div *ngIf="dataItem">
   {{dataItem |json }}
</div>

Your *ngFor wont work as it is not an array

I think this should work. The constructor is typescript thing. Try to make make API calls in Angular lifecycle hooks

Sign up to request clarification or add additional context in comments.

1 Comment

The real problem isn't getting the data, it's the fact that the OP is trying to iterate through an object.
0

Let's see what the issues are:

First You are trying to get the length of dataItem, while it's NOT an array. If you want to check the length, make sure to initialize the dataItem like this dataItem = []

<div *ngIf="dataItem?.length>=0">           

Second You are trying to iterate (loop) the dateItem while it's not an array. If you initialize it as seen in the first bullet above, you would be allowed to do so

 <div *ngFor='let x of dataItem'>

If you fix these, you will be able to see the data in the template

Have a look at this for a working example https://stackoverflow-55772639.stackblitz.io

Comments

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.