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

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

Self

decorator

Parameter decorator to be used on constructor parameters, which tells the DI framework to start dependency resolution from the local injector.

SkipSelfOptional

On this page

    arrow_upward_alt Back to the top

    API

    @Self();

    Description

    Parameter decorator to be used on constructor parameters, which tells the DI framework to start dependency resolution from the local injector.

    Resolution works upward through the injector hierarchy, so the children of this class must configure their own providers or be prepared for a null result.

    Usage Notes

    In the following example, the dependency can be resolved by the local injector when instantiating the class itself, but not when instantiating a child.

    class Dependency {}
    
            @Injectable()
            class NeedsDependency {
              constructor(@Self() public dependency: Dependency) {}
            }
    
            let inj = Injector.create({
              providers: [
                {provide: Dependency, deps: []},
                {provide: NeedsDependency, deps: [[new Self(), Dependency]]},
              ],
            });
            const nd = inj.get(NeedsDependency);
    
            expect(nd.dependency instanceof Dependency).toBe(true);
    
            const child = Injector.create({
              providers: [{provide: NeedsDependency, deps: [[new Self(), Dependency]]}],
              parent: inj,
            });
            expect(() => child.get(NeedsDependency)).toThrowError();
    Jump to details

    Web Proxy Viewer  |  New URL  |  Original Page