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
Nested multiple levels of ContentChild query returns undefined #20810
Comments
Is there any workaround for this for now? I'd like to wrap some library components to extend their behavior however this issue pretty much prevents it. Example usage of library components: <mat-table [dataSource]="dataSource">
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef>Name</mat-header-cell>
<mat-cell *matCellDef="let element">{{element.name}}</mat-cell>
</ng-container>
<ng-container matColumnDef="labels">
<mat-header-cell *matHeaderCellDef>Labels</mat-header-cell>
<mat-cell *matCellDef="let element">{{element.labels}}</mat-cell>
</ng-container>
<ng-container matColumnDef="status">
<mat-header-cell *matHeaderCellDef>Status</mat-header-cell>
<mat-cell *matCellDef="let element">{{element.status}}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table> What I'd like to have is: <kd-card-list [resource]="nodes" [sortable]="true" [filterable]="true" [pageable]="true" sortActive="name">
<div title>Nodes</div>
<kd-card-list-element name="name">
<mat-header-cell *matHeaderCellDef>Name</mat-header-cell>
<mat-cell *matCellDef="let node">{{node.name}}</mat-cell>
</kd-card-list-element>
<kd-card-list-element name="labels">
<mat-header-cell *matHeaderCellDef>Labels</mat-header-cell>
<mat-cell *matCellDef="let node">{{node.labels}}</mat-cell>
</kd-card-list-element>
<kd-card-list-element name="status">
<mat-header-cell *matHeaderCellDef>Status</mat-header-cell>
<mat-cell *matCellDef="let node">{{node.status}}</mat-cell>
</kd-card-list-element>
</kd-card-list> Unfortunately when wrapping |
/subscribe |
@floreks ... did you read ... #14769 (comment) |
So unless library specifies differently (by using |
@floreks ... it is necessary to wait till the moment when the logic of |
I understand that this is still an open issue. I was just wondering if there is any workaround right now I could try to make it work. We'll just have to wait for the fix. |
@floreks ... I suppose that the way is to use some |
That won't help as this is library function that crashes. It can not find required children using |
@floreks ... ah, I understand ... so the only chance is just completely a different way ... lib / or your own, I guess. |
I observe the same behaviour. I write my own StackBlitz to show the problem : https://stackblitz.com/edit/angular-56ruzp For me it's a major problem because if we want to deal with that, we have to passed the reference of the contentChild element to the child. So if there is an official and clean workaround, I'm intersted by it ! |
I think that there is confusion here about intention of Let me illustrate what I mean by a simple example, assuming that we've got the In the following case <my-component>
<div #foo></div>
</my-component> On the other hand we will not (and should not) "descent" into <my-component>
<div #foo></div> <!-- this element will be found -->
<ng-content></ng-content> <!-- non of the elements in the projected content gets queries, even if it contains #foo -->
</my-component> There is very simple reasoning behind this behaviour - you can think of content of a component as an additional argument / input to it. So when you use a component in your template you must control what you provide to it. You do so by sticking tags between component tags ( Descending into In short: we only query component's own content, not content coming from other templates. This makes sense since a template forms a namespace I'm going to close this one as content queries work as designed here. If you still need help with some specific use-cases please open a new issue. |
@pkozlowski-opensource , what about querying components from a projected view container? Here is a very basic setup and the code:
And the child component:
|
@MaximusK I just ran into a similar issue with 2 |
@MaximusK @jpzwarte Would be good to have a real-life use-case of what you are trying to achieve so I can propose an alternative approach. But currently queries are not checking projected nodes ( |
@pkozlowski-opensource I'm writing a
<mat-menu>
<ng-content></ng-content>
<button mat-menu-item>Log out</button>
</mat-menu>
<foo-user-menu>
<button mat-menu-item>Settings</button>
</foo-user-menu> So |
To be clear, |
I have a form component that queries all material inputs with works
doesn't (
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm submitting a...
Current behavior
When
ng-content
is used on multiple levels, only the first component is able to find the ContentChild.Expected behavior
I think every parent component at each level should be able to find the ContentChild. Please see the plunker example.
Minimal reproduction of the problem with instructions
https://plnkr.co/edit/iMDKo9cdKeyB0Y4is11y?p=preview
What is the motivation / use case for changing the behavior?
I have a
div[myFormElement]
component that projects an input. Based on what kind of input it is, I would like to add extra functionality, but I would like to separate that logic from thediv[myFormElement]
component and only include it inmy-custom-wrapper-component
.Then in the
my-form-element
component,In
my-custom-wrapper-component
,@ContentChild()
is returningundefined
.Environment
The text was updated successfully, but these errors were encountered: