Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Beginning Server-Side Application Development with Angular

You're reading from   Beginning Server-Side Application Development with Angular Discover how to rapidly prototype SEO-friendly web applications with Angular Universal

Arrow left icon
Product type Paperback
Published in Apr 2018
Publisher
ISBN-13 9781789342161
Length 110 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Bram Borggreve Bram Borggreve
Author Profile Icon Bram Borggreve
Bram Borggreve
Arrow right icon
View More author details
Toc

Creating a Service to Retrieve Data


In this section, we will use ng generate to create the PostsService, use environment to store the API URL, and use the PostsService in our components. We will then define our API calls in the PostsService and leverage the HttpClientModule to enable HTTP access.

Generating the Service

We will use the ng generate service command to generate a service that will handle the interaction with our API:

  1. Open your terminal and navigate to the project directory.

  2. Run the following command from inside the project directory:

    ng g s posts/services/posts --module posts/posts

Storing Our API URL

We will use the environment of Angular CLI to store our API URL. Using the environment, we can define a different URL for development and production environments.

By default, the application generated with Angular CLI comes with two predefined environments. These environments are defined in .angular-cli.json in the project root.

  1. Open the src/environments/environment.ts file.

  2. Inside the environment...

You have been reading a chapter from
Beginning Server-Side Application Development with Angular
Published in: Apr 2018
Publisher:
ISBN-13: 9781789342161
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime