- Create a
joke
variable inapp.component.ts
and assign this random joke:Ghosts are actually caused by Chuck Norris killing people faster than Death can process them.
- Now go to
app.component.html
and replace its content with:<h1>{{joke}}</h1>
. Now go and check the browser! 🚀
- Now it's time to start using HTTP requests! Go to
app.module.ts
and import theHttpModule
fromimport { HttpModule } from '@angular/http';
. - Actually, to start using it we need to put it in the
imports
array:imports: [BrowserModule, HttpModule],
- And the final step is to inject it in our component. Go to
app.component.ts
and create aconstructor
like this one:constructor(private http: Http) { }
- So we are almost there! Last thing we want to do is create a channel (AKA Observable):
http.get('https://api.icndb.com/jokes/random');
- Second we will subscribe to it, so we can get a new joke
http.get('https://api.icndb.com/jokes/random').subscribe();
- Finally, we want to
log
the information in theconsole
with:http.get('https://api.icndb.com/jokes/random').subscribe((response) => { console.log(response);});
- This is it, we only need to format the information correctly, replace
console.log(response)
withthis.joke = response.json().value.joke;