[ Web Proxy ]
URL:
Viewing: https://angular.dev/api/core/Injectable [Back]  [Original]

Injectable Angular Skip to main content
menu
close
more_horiz
menuAPI
@angular/core

Injectable

decorator

Decorator that marks a class as available to be provided and injected as a dependency.

Introduction to Services and DICreating and using servicesDefining dependency providers

On this page

    arrow_upward_alt Back to the top

    API

    Usage Notes

    Marking a class with @Injectable ensures that the compiler will generate the necessary metadata to create the class's dependencies when the class is injected.

    The following example shows how a service class is properly marked so that a supporting service can be injected upon creation.

    @Injectable()
            class UsefulService {}
    
            @Injectable()
            class NeedsService {
              constructor(public service: UsefulService) {}
            }
    
            const injector = Injector.create({
              providers: [
                {provide: NeedsService, deps: [UsefulService]},
                {provide: UsefulService, deps: []},
              ],
            });
            expect(injector.get(NeedsService).service instanceof UsefulService).toBe(true);
    Jump to details

    Web Proxy Viewer  |  New URL  |  Original Page