0

When generating a service using Angular CLI (ng g s my-service) it by default provides it in root:

@Injectable({
  providedIn: 'root'
})
export class MyService {
...

Is there an way to change the setting of the CLI to generate the service without that?

@Injectable()
export class MyServiceService {
...

EDIT

I know I can define a module to provide it with a --module flag, I'm asking if:

(1) It is possible not to provide the service at all.

(2) Change the default behavior of the CLI - so it doesn't provide by default, or provide in a module by default (without using the module flag) (It behaved differently in earlier versions)

Shachar Har-Shuv
  • 666
  • 6
  • 24
  • possible duplicate of https://stackoverflow.com/questions/42748773/angular-cli-generate-a-service-and-include-the-provider-in-one-step – Muhammad Ahsan Dec 31 '18 at 08:08
  • 1
    Possible duplicate of [Angular cli generate a service and include the provider in one step](https://stackoverflow.com/questions/42748773/angular-cli-generate-a-service-and-include-the-provider-in-one-step) – Muhammad Ahsan Dec 31 '18 at 08:09
  • 1
    It is not configurable in service schematics, see https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/service/files/__name%40dasherize%40if-flat__/__name%40dasherize__.service.ts – Vitalii Bobrov Dec 31 '18 at 09:55
  • 1
    created feature request https://github.com/angular/angular-cli/issues/13335 – Vitalii Bobrov Dec 31 '18 at 10:20

1 Answers1

0

You can use the ----module option while creating the service and specify the module where you want to provide the service. For your case to provide in NgModule use the following:

ng g s services/MyService --module=app.module

Jahnavi Paliwal
  • 1,721
  • 1
  • 12
  • 20