NG.IV · Angular 22 practice question

A development team is building a large Angular application and has several utility services. They are debating whether to register these services using the `providers` array within `ApplicationConfig` or by applying a specific decorator to the service class itself. Based on modern Angular practices, what is the recommended approach for these services?

import {Service} from '@angular/core';

@Service()
export class DataHelperService { /* ... */ }
Choose one answer
Check answer and explanation

Correct answer: Applying the specific decorator to the service class, as this allows optimization tools to effectively remove unused code.

Explanation

The preferred method is 'Applying the specific decorator to the service class, as this allows optimization tools to effectively remove unused code.' because the documentation indicates that employing `@Service()` enables optimization tools to perform tree-shaking, leading to reduced bundle sizes.

Common trap

The suggestion to 'Always listing the service in the `providers` array within `ApplicationConfig` to ensure global availability across all modules' is flawed because, although `ApplicationConfig.providers` can configure the injector, the documentation strongly favors the decorator for superior tree-shaking capabilities. Furthermore, the assertion that 'Using the `EnvironmentInjector` configuration exclusively, as it is the only recognized method for service provisioning' is too restrictive, as the decorator represents the preferred configuration technique.

Source: Angular docs: Hierarchical injectors — `EnvironmentInjector`
Checked against the cited text on 2026-11-25.

Report an error in this question