0

I need to fetch JSON data to the array, and with the press of a button to create a new list. Any solutions?

import { Component } from '@angular/core';
import { RandomService } from '../services/random.service';

@Component({
selector: 'pocetna',
template: `
<ul>
<li *ngFor="let joke of jokes">{{joke.value}}</li>
</ul>
<button (click)="getJoke()">more jokes</button>`,

providers: [RandomService]
})
export class PocetnaComponent  { 
jokes:Joke[] = [];

constructor(private jokesService: RandomService){}

ngOnInit() {
this.getJoke()
}

getJoke() {
this.jokesService.getRandomJokes()
.subscribe(joke => {
   this.jokes.unshift(joke)
})    
}
}
interface Joke{
id: number;
value: string;
}

This is how my service looks like, I think I'm missing something in it

import { HttpModule } from '@angular/http';
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class RandomService {
constructor(private http: Http){
console.log('working');
}
getRandomJokes(){
 return this.http.get('https://api.chucknorris.io/jokes/random')
.map(res => res.json()) 
.map(joke => <Joke>{id: joke.id, value: joke.value});
}

}

This code worked before restarting application but now this line of code says it can't find :

.map(joke => <Joke>{id: joke.id, value: joke.value});
17
  • Does your solution work? Your question doesn't seem very clear. Commented May 26, 2017 at 9:17
  • it's not working, it isn't fetching any data Commented May 26, 2017 at 9:19
  • What do you mean with create a new list? Commented May 26, 2017 at 9:25
  • with a press of a button to push an API into the array and display it as an new <li> Commented May 26, 2017 at 9:26
  • 1
    Possible duplicate of Angular 2 onclick add new item in array Commented May 26, 2017 at 9:35

3 Answers 3

0

I think you're trying to convert the json returned in the api to array loop through the json and convert it into array

 loadMore() {
        this.jokes = this.service.getRandomJokes()
            .subscribe(joke => { 
 const getdata = [];
 for(let key in data){
getdata.push(data[key]);
} 
this.jokes =getdata
});
    }
Sign up to request clarification or add additional context in comments.

Comments

0

https://api.chucknorris.io/jokes/random it's response a object.

Make response with a array

[
    {
      "category": null,
      "icon_url": "https://assets.chucknorris.host/img/avatar/chuck-norris.png",
      "id": "RPVWYzLySH277dwK-Se3PQ",
      "url": "http://api.chucknorris.io/jokes/RPVWYzLySH277dwK-Se3PQ",
      "value": "Chuck Norris is allowed to draw pictures of Mohammad"
    }
]

Comments

0

I guess that you've got problem with parsing data in the response.

You can try this:

.map(res => res.json())
  .subscribe(
      data => console.log(data),
      err => console.log(err),
      () => console.log('Random Quote Complete')
  );

OR:

.map((res: Response) => {
  let data = (res.json() || {}).data;
  return data;
});

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.