Skip to content

ViewChild on component placed inside of template being used by other component is always set to same component #14835

Closed

Description

@marclerman

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 => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior
Please look at this Plunker.
This test case shows that the ViewChild for a component placed inside of a template used by another component will remain unchanged, although the actual component follows the expected lifecycle (construction, OnInit, OnDestroy).

Expected behavior
I expected that the ViewChild variable would be updated according to the InnerComponent actually in the DOM.

Minimal reproduction of the problem with instructions

  • Go to the Plunker.
  • When the application loads, we only have a "Toggle content" button. Click it, and a new InnerComponent (of index 1) will be constructed, and will go through its OnInit. The ViewChild for #inner in AppComponent is set to that InnerComponent of index 1.
  • Click "Toggle content" again, and the InnerComponent of index 1 will have its OnDestroy called and it will be removed from the DOM.
  • Click "Toggle content" again, and a new instance of the InnerComponent (of index 2) will be constructed, and its OnInit will be called. However, #inner will be set again to first InnerComponent of index 1.
  • Click "Toggle content" again, and the InnerComponent of index 2 will have its OnDestroy called and it will be removed from the DOM.
  • Every time you will make InnerComponent appear, #inner will still be called with the same InnerComponent of index 1.

What is the motivation / use case for changing the behavior?
It is a big problem that what we see in the browser and the component we get at through the ViewChild are not the same. ViewChildren seems to be exhibiting the same problem.

Please tell us about your environment:
Windows 10

  • Angular version: 2.4.8
  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
    All

  • Language: [all | TypeScript X.X | ES6/7 | ES5]
    Typescript 2.0.3

  • Node (for AoT issues): node --version =

Activity

  1. changed the title [-]ViewChild on component placed inside of template being used by other component always is set to same component[/-] [+]ViewChild on component placed inside of template being used by other component is always set to same component[/+] on Mar 1, 2017
  2. alexzuza commented on Mar 1, 2017

    @alexzuza
    Contributor

    Maybe it is related somehow with this issue #13921

    In your case you can try using the following workaround:

    TogglerComponent

    <template *ngIf="_isShowing" #ngTemplateOutletContainer [ngTemplateOutlet]="_content?.templateRef"></template>
    
    @ViewChild('ngTemplateOutletContainer', { read: ViewContainerRef })
    private ngTemplateOutletContainer: ViewContainerRef;
    
    public toggle(): void {
     if(this.ngTemplateOutletContainer) {
       this.ngTemplateOutletContainer.clear();
     }
     this._isShowing = !this._isShowing;
    }
    
    

    https://plnkr.co/edit/8RJHdsFAXtoylE01pVZi?p=preview

  3. marclerman commented on Mar 2, 2017

    @marclerman
    Author

    Great workaround, thanks very much @alexzuza!

    Of course, it looks like this should be fixed in Angular itself.

  4. added
    area: core
    on Mar 13, 2017
  5. amcdnl commented on Mar 27, 2017

    @amcdnl
    Contributor

    Can anyone confirm if this is an issue in ng4 still?

  6. added this to the Backlog milestone on Jan 23, 2018
  7. pkozlowski-opensource commented on Mar 23, 2019

    @pkozlowski-opensource
    Member

    I can't reproduce it any more with the latest Angular: https://stackblitz.com/edit/angular-p6jhzm
    Alternatively, I might be misunderstanding the issue :-)

    I'm going to close it for now as I can't reproduce but please feel free to open a new issue with a minimal stackblitz if anyone thinks that things are still not working as expected.

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

  9. locked and limited conversation to collaborators on Sep 14, 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

    area: corefreq2: mediumtype: bug/fix

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions