New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accessing @ViewChild and @ViewChildren of a template declared in a different component than the one where it is host #43946
Comments
I found a way to get the references of the views inside the template but my solution is not perfect and it has some drawbacks:
So the solution is to get the references from inside the parent: parent.component.ts
The child will then get a reference to the parent thanks to child.component.ts
Because the hello.component.ts
The parent component can distinguish parent.component.html
The child component can now filter parent's views to get what it is searching for. child.component.ts
As I mentioned before, this is working but is has a few drawbacks that should be addressed. I created a |
This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list. You can find more details about the feature request process in our documentation. |
Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends. Find more details about Angular's feature request process in our documentation. |
Please!!! Vote for this feature with a thumbs up in the first post!!! It will be super helpful!!! :(! |
Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage. We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package. You can find more details about the feature request process in our documentation. |
So.. there is still hope :-) |
Hello! Can you provide more context on why you want this feature? What problem are you trying to solve that you can't solve today? |
i can probably. We have something like this.
we can get all of the templates with the help of that *field directive - so dynamic-form has all of the templates so it can provide them for the dynamic-node components as well.
The problem is, we cannot get instance of that base-component from dynamic-node... it is not accessible neither by dynamic-node.contentChildren / dynamic-node.viewChildren BaseComponent instance is only accesible by Dynamic-form.. and that looks little bit odd to me... I know, ng-template is in the context of the parent template.. however.. this is instance, which is created INSIDE (because ngTemplateOutlet is inside dynamic-node... So why we cannot access that instance via view/content children in dynamic-node? It is pretty much viewchild / content child of dynamic-node component as well. |
Hello guys. I got here looking for an awnser to why i couldnt have access to ViewChildrens that were injected inside ng-template from a Parent component. I have a componente called So when im building for example a UsersForm, i use FormView with a <UsersForm>
<FormView entity='users' >
<ng-template
#body
let-fields="fields"
let-results="results"
let-formStore="formStore"
> With those fields and results i can create whatever UI want from parent UsersForm. So i use those fields as prop to my <UsersForm>
<FormView entity='users' >
<ng-template
#body
let-fields="fields"
let-results="results"
let-formStore="formStore"
>
<app-form-generator
[fields]="fields"
[columns]="userColumns"
[fieldsValidators]="userValidators"
>
Everything worked nice when im at the "create mode" for the I can still pass [results] to my FormGenerator or maybe get those form references information via service. Im new to Angular world so trying to build things that i used to see in Vue world. |
When I use templates nested, I want to access the component tree through the injector. But after the current template is nested, the tree structure of the content cannot be obtained at all. I don't know why the template is designed like this, which is a bit contrary to common sense. |
Any updates? |
Any solutions or workarounds? |
There are lot of annoying workarounds like sharing with CustomService and posting injector... But... these issues really complicating basic component composition are usually ignored by Angular team. We have to wait 6-7 years, like in case of Reactive forms and their events.... So just start a family, build a house, plant a tree and wait with hope. |
Which @angular/* package(s) are relevant/releated to the feature request?
common, core
Description
We can't access template's view children from the component that host the template via
ngTemplateOutlet
if the template is not declared inside this same component.For example:
TemplateRef
as a@Input()
and insert this template throughngTemplateOutlet
.child.component.html
child.component.ts
parent.component.html
The log in child component returns
false
.I already made a
StackOverflow
post at https://stackoverflow.com/q/69709254/3520621.Their is a
StackBlitz
available for reproductivity at https://stackblitz.com/edit/angular-eopzyw?file=src/app/app.component.ts.Proposed solution
I'm not sure of what could be a proper solution but as Ritesh Waghela mentioned in the
StackOverflow
post, the template content has the samehost id
as the parent component and not the same as the child component.Maybe the template content should in this case be attached to the child and not the parent ? But I'm not sure if this won't break any actual behaviour.
In this example the
ngcontent host id
shoulld have beenlhp-c2
instead oflhp-c0
.Alternatives considered
In case the template should be use only once, using the power of Content-Projection should do the trick but in my real-life context the template should be reusable. The
child-component
must be able to set angTemplateOutletContext
.Anyway, I'm open to any proposition of an alternative way to access the template's views from child component by keeping the dynamic and reusable power of
ng-template
.Thanks.
The text was updated successfully, but these errors were encountered: