I'm trying to fetch data from this URL:https://api.chucknorris.io/jokes/search?query=chuck (chuck is the search term, it can be anything else).
I want to get the value property from the array. Postman gets me this:
{
"total": 9735,
"result": [
{
"category": [
"dev"
],
"icon_url": "https://assets.chucknorris.host/img/avatar/chuck-norris.png",
"id": "zdj0bfkjsmup6pkb2rpmbw",
"url": "http://api.chucknorris.io/jokes/zdj0bfkjsmup6pkb2rpmbw",
"value": "Chuck Norris is the only human being to display the Heisenberg
uncertainty principle - you can never know both exactly where and how
quickly he will roundhouse-kick you in the face."
},
my service(edited):
import { HttpModule } from '@angular/http';
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import { Joke } from '../joke'
@Injectable()
export class PretragaService {
constructor(private http: Http){}
searchJokes(str: string){return
this.http.get('https://api.chucknorris.io/jokes/search?query=' + str)
.map(res => res.json())
.map(joke => {return joke.value});
}
}
Component(edited):
import { Component } from '@angular/core';
import { PretragaService } from '../services/pretraga.service';
@Component({
selector: 'pretraga',
template: `
<input type="text" class="form-control" placeholder="pretraži"
[(ngModel)]="searchJoke"
name="searchJoke" (keyup)="searchJokes()">
<div *ngIf="searchRes">
<div *ngFor="let joke of searchRes">
<ul>
<li>{{joke}}</li>
</ul>
</div>
</div>
`,
providers: [PretragaService]
})
export class PretragaComponent {
searchJoke: string;
searchRes: any[]=[];
constructor(private searchService: PretragaService){
}
searchJokes(){
this.searchService.searchJokes(this.searchJoke)
.subscribe(joke => {
this.searchRes = joke;
});
}
I have the class that represents an object:
export class Joke{
value: string;
}