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});