Skip to content

Injector.get throws when default value is provided (when inject flags are present) #47109

Closed

Description

@CSchulz

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

Yes

Description

The injector.get documentation explains the usage of the notFoundValue and how it is handled.

/**
* Retrieves an instance from the injector based on the provided token.
* @returns The instance from the injector if defined, otherwise the `notFoundValue`.
* @throws When the `notFoundValue` is `undefined` or `Injector.THROW_IF_NOT_FOUND`.
*/
abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T;

But the implementation requires the usage of InjectorFlags.Optional

function notFoundValueOrThrow<T>(
notFoundValue: T|null, token: ProviderToken<T>, flags: InjectFlags): T|null {
if (flags & InjectFlags.Optional) {
return notFoundValue;
} else {
throwProviderNotFoundError(token, 'NodeInjector');
}
}

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

Error: NG0201: No provider for NgControl found in NodeInjector. Find more at https://angular.io/errors/NG0201
    at throwProviderNotFoundError (node_modules\@angular\core\fesm2020\core.mjs:254:11)
    at notFoundValueOrThrow (node_modules\@angular\core\fesm2020\core.mjs:3349:9)
    at lookupTokenUsingModuleInjector (node_modules\@angular\core\fesm2020\core.mjs:3384:12)
    at getOrCreateInjectable (node_modules\@angular\core\fesm2020\core.mjs:3486:12)
    at NodeInjector.get (node_modules\@angular\core\fesm2020\core.mjs:3661:16)

Please provide the environment you discovered this bug in (run ng version)

The implementation didn't change on the master branch.

Angular CLI: 13.3.9
Node: 16.14.2
Package Manager: npm 8.5.0
OS: win32 x64

Angular: 13.3.11
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1303.9
@angular-devkit/build-angular   13.3.9
@angular-devkit/core            13.3.9
@angular-devkit/schematics      13.3.9
@angular/cli                    13.3.9
@schematics/angular             13.3.9
ng-packagr                      13.3.1
rxjs                            7.5.6
typescript                      4.6.4

Anything else?

No response

Activity

  1. modified the milestone: needsTriage on Aug 10, 2022
  2. bob-watson commented on Aug 10, 2022

    @bob-watson
    Contributor

    I added core:di as the documentation is in the code and not in the docs source.
    I'm not sure if that means it should have other labels, as well.
    I also added state: needs eng input so that it can be directed to the best person/group.

  3. added
    P3
    on Aug 10, 2022
  4. modified the milestones: needsTriage, Backlog on Aug 10, 2022
  5. AndrewKushnir commented on Aug 11, 2022

    @AndrewKushnir
    Contributor

    @CSchulz I've created a quick demo to illustrate some of the cases around the default value:

    https://stackblitz.com/edit/angular-ivy-j7gpll?file=src%2Fapp%2Fapp.component.ts

    The behavior looks correct and corresponds to the docs.

    Could you please provide additional information about your proposal on how to improve the docs?

  6. added
    needs: clarification
    on Aug 11, 2022
  7. CSchulz commented on Aug 12, 2022

    @CSchulz
    Author

    Thanks for the example, in my case I used the InjectFlags.Host modifier:

    const d = injector.get(NON_PROVIDED_TOKEN, 'default value', InjectFlags.Host);
    console.log('d: ', d);
    
  8. AndrewKushnir commented on Aug 12, 2022

    @AndrewKushnir
    Contributor

    @CSchulz thanks for additional information. This actually looks like a bug: it's unexpected that the injector.get would throw in this case:

    const d = injector.get(NON_PROVIDED_TOKEN, 'default value', InjectFlags.Host);
    

    I've also updated my Stackblitz demo. I update labels on this issue as well.

  9. added
    area: core
    and removed
    needs: clarification
    on Aug 12, 2022
  10. changed the title [-]Injector.get documentation differs in implementation[/-] [+]Injector.get throws when default value is provided (when inject flags are present)[/+] on Aug 12, 2022
  11. added a commit that references this issue on Aug 24, 2022
    82e6c89
  12. added a commit that references this issue on Sep 8, 2022
    a3e1303
  13. added a commit that references this issue on Sep 18, 2022
    282c642
  14. angular-automatic-lock-bot commented on Oct 9, 2022

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  15. locked and limited conversation to collaborators on Oct 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

P3area: corecore: distate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions