Skip to content

Missing forwardRef in Query/ViewQuery should throw #4174

Closed

Description

@ericmartinezr

Consider the following code

@Component({selector: 'accordion'})
@View({
  template: `
        <accordion-item></accordion-item>
        <accordion-item></accordion-item>`,
  directives : [forwardRef(() => AccordionItem)]
})
export class Accordion implements OnInit {
    constructor(el: ElementRef, @ViewQuery(AccordionItem) items:QueryList<AccordionItem>) {
          this.items = items;
    }
    onInit() {
         console.log(this.items);
    }
}

@Component({selector: 'accordion-item'})
@View({template: `...`})
export class AccordionItem {}

This works, but items returns 0 results, which is wierd since there are two children (see plnkr below). The problem here is a missing forwardRef.

If you remove forwardRef from the directives property in Accordion you'll get this error message. Pretty useful.

Unexpected directive value 'undefined' on the View of component 'Accordion'

But if you miss forwardRef in @ViewQuery instead of an error you'll get 0 results, while it should throw a similar error to the above one.

See repro, switch between the constructors in Accordion class and you'll see that no errors are thrown but the length of the results changes.

PS: The title includes Query which I didn't test, but I'm assuming that both works similar.

Activity

  1. jessegood commented on Sep 14, 2015

    @jessegood

    Agreed. It would be nice if Angular told you when the type is undefined inside of @ViewQuery.
    Also, maybe in the future the TypeScript transpiler will add support for checking before use:
    microsoft/TypeScript#4114

  2. 0x-r4bbit commented on Sep 17, 2015

    @0x-r4bbit
    Contributor

    Just mentioning here that @Query and @ViewQuery are going away in favour of @ContentChildren and @ViewChildren via #4237

    Your issue might remain tho.

  3. ericmartinezr commented on Oct 3, 2015

    @ericmartinezr
    ContributorAuthor

    Closing it in favor of #4489

  4. angular-automatic-lock-bot commented on Sep 7, 2019

    @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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions