Ace the Angular Interview 2025 – Code Your Way to Success!

Image Description

Question: 1 / 400

Which command is used to create a new service in Angular?

ng create service service-name

ng generate service service-name

The command used to create a new service in Angular is "ng generate service service-name." This command is part of Angular's CLI (Command Line Interface) tool, which provides a simplified way to scaffold different elements of an Angular application, including components, directives, and services.

When you use "ng generate service service-name," the Angular CLI performs the following tasks:

1. **File Creation**: It automatically creates the necessary files for the service, including an Angular service file (.ts) and a spec file (.spec.ts) for testing, often placing them in the appropriate directory structure based on your specified path.

2. **Service Registration**: If you create the service in a module, Angular can automatically handle the registration of the new service in that module, allowing for dependency injection to work seamlessly.

3. **Boilerplate Code**: The CLI generates standard boilerplate code that follows best practices, reducing the chances of errors and saving time.

Using the other commands mentioned would not achieve the same result. For instance, the possibility of creating a service using "ng create service service-name," "ng add service service-name," or "ng new service service-name" does not align with Angular’s CLI commands and structure, reflecting their specific functionalities

Get further explanation with Examzify DeepDiveBeta

ng add service service-name

ng new service service-name

Next Question

Report this question

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy