Skip to content

fix: better error messages for NgFor #14641

Description

@0x-r4bbit

I'm submitting a ... (check one with "x")

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request

Current behavior
While there have been a lot of improvements in the error messages space in Angular, it turns out that there's one case left that can causes many hours of debugging, because the error message is confusing.

When forgetting the let keyword in NgFor's micro syntax, that error message points the developer in a wrong direction. Basically, doing this:

<ANY *ngFor="item of items"></ANY>

Throws this error:

Can't bind to 'ngFor' since it isn't a known property of 'ANY'.
1. If 'ANY' is an Angular component and it has 'ngFor' input, then verify that it is part of this module.
2. If 'ANY' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message.

This error messages usually occurs when one forgets to add/export a directive/component (in this case NgFor), but the actually issue is that the developer forgot the let keyword in the micro syntax.

Unfortunately this caused me once 1.5 hours of debugging and wondering why Angular thinks I haven't declared NgFor anywhere, while it is in fact properly setup.

I'm seeing more and more people running into this, so I thought it's worth bringing up.

Expected behavior
It would be cool if we can throw a more specific error message, or maybe extend the existing one to tell the developer that she needs to double-check the micro syntax.

**Plunker for reproduction

https://plnkr.co/edit/SAI2cYUjuiVXcg4sL8vz?p=preview (check the console)

  • Angular version: 2.x.

Activity

  1. DzmitryShylovich commented on Feb 22, 2017

    @DzmitryShylovich
    Contributor

    pls add a plunkr

  2. 0x-r4bbit commented on Feb 22, 2017

    @0x-r4bbit
    ContributorAuthor

    @DzmitryShylovich added to the description.

  3. DzmitryShylovich commented on Feb 23, 2017

    @DzmitryShylovich
    Contributor

    @vicb is item of items a valid expression? I'm trying to figure out the right place for the fix. I think Parser should throw

  4. BrunoViera commented on Apr 2, 2017

    @BrunoViera

    Hello! some news about it? I have the same error

  5. Toxicable commented on Apr 8, 2017

    @Toxicable

    @BrunoViera to fix this change <ANY *ngFor="item of items"></ANY> into <ANY *ngFor="let item of items"></ANY> the part you were missing was the let.
    This issue however is about improving the error messages that it throws when this situation does occur

  6. emeldar commented on Apr 12, 2017

    @emeldar

    So I gave at shot at a fix for this. Bear in mind that this is my first time doing something like this so feel free to just shut me down if I'm wrong :) I'm not entirely sure how to link the pull request I made :( But it is the only pull request in the compiler-builds repo

  7. Toxicable commented on Apr 12, 2017

    @Toxicable

    @eldaromer any PR to Angular should be made in this repo
    eg https://github.com/angular/angular/pulls

  8. emeldar commented on Apr 12, 2017

    @emeldar

    @Toxicable sorry, I'll delete the other one and fix it, thank you!

  9. doctorstal commented on May 10, 2017

    @doctorstal

    Same with 'ngIf' directive. You get same message if directive syntax is messed up.
    Plunker: https://plnkr.co/edit/y1R2KaRRnfMFXJH2mCe1

  10. Math3v commented on Jun 7, 2017

    @Math3v

    @eldaromer @Toxicable Is this fixed, or can I take a look at it?

  11. Toxicable commented on Jun 8, 2017

    @Toxicable

    @Math3v it's not fixed, feel free to make a PR for it

  12. Math3v commented on Jun 8, 2017

    @Math3v

    @Toxicable PR is in place.

  13. added this to the Backlog milestone on Jan 24, 2018
  14. removed
    open for contributionsAn issue that is suitable for a community contributor (based on its complexity/scope).
    on May 19, 2020
  15. added
    P4A relatively minor issue that is not relevant to core functions
    and removed on Oct 1, 2020
  16. jessicajaniuk commented on Aug 8, 2022

    @jessicajaniuk
    Contributor

    This issue should now be fixed with a new extended template diagnostic.

  17. angular-automatic-lock-bot commented on Sep 8, 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.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions