Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Julie Gladden
Julie Gladden

Posted on

     

HttpClient 'GET' - Angular Simplified

Hey, friends.

Today I want to break down Angular's method of calling APIs. I'll be doing an article for each CRUD operation, so keep your eye out and make sure to follow!
This is the basics, and you can find more info on Angular's website. I won't get into the nitty gritty of why and how things work here. As a beginner, sometimes too much information is a bad thing. So let's start simple and grow from there!

Step 1:

Time to import!

In your app.module file...

import{HttpClientModule}from'@angular/common/http';@NgModule({imports:[BrowserModule,HttpClientModule,<--addtoimportshere],
Enter fullscreen modeExit fullscreen mode

In your .ts file...

import{HttpClient}from'@angular/common/http';
Enter fullscreen modeExit fullscreen mode

Step 2:

Next we need to add to our constructor.

In your .ts file...

constructor(privatehttp:HttpClient){}
Enter fullscreen modeExit fullscreen mode

Step 3:

Now it's time to call our API!

Here's our main functionality. Inside of the get parenthesis, we are going to put our URL or anything that needs to be sent over. Consider subscribe to be what actually plugs us in to the service. If you don't have it, we won't have the power to turn this function on. It'll give us back the data we need so we can use it elsewhere.

functionName(){this.http.get().subscribe()}
Enter fullscreen modeExit fullscreen mode

Now add your URL to it.

myUrl='https://www.heythisiscool.com/1'functionName(){this.http.get(myUrl).subscribe()}
Enter fullscreen modeExit fullscreen mode

Next, you can do stuff in your subscribe parenthesis. You could console.log to see what you get back.

functionName(){this.http.get(myUrl).subscribe(data=>{console.log(data)}}
Enter fullscreen modeExit fullscreen mode

Or you can put data in a variable.

myVariable;functionName(){this.http.get(myUrl).subscribe(data=>{myVariable=data})}
Enter fullscreen modeExit fullscreen mode

You can also do multiple things on subscribe. These things won't happen until the call is complete, so it's handy in many situations.

myVariable;functionName(){this.http.get(myUrl).subscribe(data=>{myVariable=data,console.log(data),callAnotherFunction(),setAVariable=true}}
Enter fullscreen modeExit fullscreen mode

There's many other things to cover with get requests, so many sure to follow for the nitty gritty in the future!

Best wishes,
Julie

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hello,I'm a full stack engineer just doing my thing. Currently simplifying Angular concepts. Follow along for the ride. :)
  • Work
    Software Developer
  • Joined

More fromJulie Gladden

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp