Angular

Angular CLI Basics - ng generate service

PRO

In this video, we cover how to generate services using the Angular CLI. To generate a service, you need to run the following command:

ng generate service service-name

You can add a number of flags after the component name, depending on what you need. The flags we covered in this section are:

  • --skipTests
  • --flat

Remember that when generating services with the CLI, modules will not be updated. That's because in the latest versions of Angular, services have a setting that is added to the decorator on a service that makes the service be provided in a location of the application. By default, that location is "root". That's the preferred method of creating services, so when you add a service to a folder it's done for organization of code only. Don't expect to see modules updated after adding a new service.

For more information on generating services, and for a list of all the flags available when generating services, you can check out the docs.

If you'd like to learn more about services in general in Angular, and their roles in an application, read this section in the Angular documentation.