0

I'm making http request in angular. but values wont display in web page and also i'm using sample free api - https://reqres.in/api but this wont display any values.

here is my component.ts

 @Component({
      selector: 'app-value',
      templateUrl: './value.component.html',
      styleUrls: ['./value.component.css']
    })
    export class ValueComponent implements OnInit {

      values: any;
      constructor(private http: HttpClient) { }

      ngOnInit() {
        this.getValues();
      }

      getValues() {
      return this.http.get<any>('https://reqres.in/api/users/2').subscribe(response => {this.values = response.any; });

      }

    }

and here is component.html

 <ul>
    <li *ngFor="let value of values">
      {{ value.text }}
    </li>
  </ul>
1
  • Why do you use 'response.any'? Response is a JSON object with a key of 'data'. Commented Jun 6, 2019 at 8:36

2 Answers 2

1

If you get single record, do not user ngFor.

Change response.any to response.data beacuse api returns in JSON 'data' as key not 'any' then in template add for example:

<ul>
    <li>
      {{ values.email}}
    </li>
  </ul>

ngFor you can use, when api return data as object array. Very good solution is using Postman, to check what data API returns, to know what the JSON structure looks like.

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

4 Comments

thanks. it worked. i'm a beginner for this and really appreciated for the help :)
No problem :) Good luck with angular.
how do i change this if i need to bring list? should i move to ngFor? is there any change need to be done in component.ts?
Yes, then you must user ngFor. In component you do not have to change anything.
1

this.values = response.any is wrong because your response is an object which has property data not any.

Also at this endpoint, you are getting a single user, not a collection of users, so you should rename your variable to value not values.

 @Component({
      selector: 'app-value',
      templateUrl: './value.component.html',
      styleUrls: ['./value.component.css']
    })
    export class ValueComponent implements OnInit {

      value: any;
      constructor(private http: HttpClient) { }

      ngOnInit() {
        this.getValue();
      }

      getValue() {
      return this.http.get<any>('https://reqres.in/api/users/2').subscribe(response => {this.value = response.data; });

      }

    }

In HTML you can use the json async pipe to present your data as json

<div> {{ value | json}} </div>

2 Comments

thanks for the help. changed response.any to response.data and removed ngFor. now it works fine.thanks :)
how do i change this if i need to bring list? should i move to ngFor? is there any change need to be done in component.ts?

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.