You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ViewChild on component placed inside of template being used by other component is always set to same component #14835
[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
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
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
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.
Activity
[-]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[/+]alexzuza commentedon Mar 1, 2017
Maybe it is related somehow with this issue #13921
In your case you can try using the following workaround:
TogglerComponent
https://plnkr.co/edit/8RJHdsFAXtoylE01pVZi?p=preview
marclerman commentedon Mar 2, 2017
Great workaround, thanks very much @alexzuza!
Of course, it looks like this should be fixed in Angular itself.
amcdnl commentedon Mar 27, 2017
Can anyone confirm if this is an issue in ng4 still?
pkozlowski-opensource commentedon Mar 23, 2019
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.
angular-automatic-lock-bot commentedon Sep 14, 2019
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.