Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Injectable } from '@angular/core';

@Injectable({
<% if (!skipProvidedIn) { %>@Injectable({
providedIn: 'root',
})
export class <%= classifiedName %> {

}
})<% } else { %>@Injectable()<% } %>
export class <%= classifiedName %> {}
18 changes: 18 additions & 0 deletions packages/schematics/angular/service/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,22 @@ describe('Service Schematic', () => {
expect(content).toContain('export class FooService {');
expect(testContent).toContain("describe('FooService', () => {");
});

it('should allow skipping providedIn when skipProvidedIn is true', async () => {
const options = { ...defaultOptions, skipProvidedIn: true };

const tree = await schematicRunner.runSchematic('service', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.ts');
expect(content).not.toMatch(/providedIn/);
expect(content).toMatch(/^@Injectable\(\)$/m);
});

it('should include providedIn: "root" by default', async () => {
const options = { ...defaultOptions };

const tree = await schematicRunner.runSchematic('service', options, appTree);
const content = tree.readContent('/projects/bar/src/app/foo/foo.ts');
expect(content).toMatch(/providedIn: 'root'/);
expect(content).toMatch(/^@Injectable\({$/m);
});
});
5 changes: 5 additions & 0 deletions packages/schematics/angular/service/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
"description": "Skip the generation of a unit test file `spec.ts` for the service.",
"default": false
},
"skipProvidedIn": {
"type": "boolean",
"default": false,
"description": "When true, does not add the providedIn property to the @Injectable decorator. The service must then be provided manually"
},
"type": {
"type": "string",
"description": "Append a custom type to the service's filename. For example, if you set the type to `service`, the file will be named `my-service.service.ts`."
Expand Down