1

I need to get data from API by submit button

Here is url - https://api.chucknorris.io/jokes/search?query=******

***** is the value from the input

This is the response from API

{
"total": 1,
"result": [
    {
        "category": null,
        "icon_url": "https://assets.chucknorris.host/img/avatar/chuck-norris.png",
        "id": "cq6hLP0ETeW4VSrm7SYg5A",
        "url": "https://api.chucknorris.io/jokes/cq6hLP0ETeW4VSrm7SYg5A",
        "value": "Chuck Norris knows WAZZZUP!"
    }
]

}

I need to get value from the result and show it in View

As you can see it's an object, not array.

I tried to it like this

Template code

<div class="container-fluid">
  <form class="form-inline" (ngSubmit)="search()" [formGroup]="searchForm" novalidate>
      <label for="text">Enter value:</label>
      <input formControlName="jokevalue" style="margin-left:20px;" type="text" class="form-control" id="email">
      <button style="margin-left:20px;" type="submit" class="btn btn-primary">Submit</button>
  </form>
  <div style="background: black; height: 60px;" *ngFor="let joke of jokes;index as i">
      <p style="color: white">{{i}}</p>
      <p style="color: white">{{joke.result[i].value}}</p>
  </div>

Component code

   import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { finalize } from 'rxjs/operators';
import { Http, Headers, Response, URLSearchParams } from '@angular/http';
import { HttpClient } from '@angular/common/http';

import { environment } from '@env/environment';
import { Logger, I18nService, AuthenticationService } from '@app/core';

@Component({
  selector: 'app-testcomponent',
  templateUrl: './testcomponent.component.html',
  styleUrls: ['./testcomponent.component.scss']
})
export class TestcomponentComponent implements OnInit {
  public jokes: any;
  version: string = environment.version;
  error: string;
  searchForm: FormGroup;

  constructor(
    private formBuilder: FormBuilder,
    private http: HttpClient
    ) {
    this.createForm();
   }

  ngOnInit() {}

  search() {
    this.http.get('https://api.chucknorris.io/jokes/search?query='+this.searchForm.value.jokevalue ).subscribe(
    data => [
      console.log(data),
      this.jokes = data

    ])
  }

  private createForm() {
    this.searchForm = this.formBuilder.group({
      jokevalue: ['', Validators.required],
    });
  }
}

But it says

Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

How I can. fix this?

Thank's for help.

1
  • 2
    pass data.result instead of data Commented Jul 31, 2018 at 15:04

2 Answers 2

5

The jokes array is in data.result.

this.jokes = data.result should work.

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

3 Comments

Lol. Really, why I not mentioned it before
It works. But it says error TS2339: Property 'result' does not exist on type 'Object'.. But it works
Typescript is stupid with that. Use data["result"] syntax as a workaround.
1

You are iterating over the response object instead of the results in the response object.

Change this in your template: *ngFor="let joke of jokes.result;index as i"

Or you can change your component to: this.jokes = data.result

5 Comments

It works with change in component. But it says error TS2339: Property 'result' does not exist on type 'Object'.. But it works
When you print your object change this : {{joke.result[i].value}} to this {{joke.result[i]?.value}}, it will prevent missing value error
I talk about component @LeoR.
Try: this.http.get<any>('api.chucknorris.io/jokes/… )
Yeah. Works. Thanks

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.