Skip to content

ContentChildren doesn't get children created with NgTemplateOutlet #14842

@tolemac

Description

@tolemac

Original title: ViewChildren doesn't get children created with NgTemplateOutlet

Please see comments below...

I'm submitting a ...

[x] bug report

Current behavior
When you use ngTemplateOutlet to render a template passing by input property ViewChildren don't get or don't find in the redered elements.

Expected behavior
I think ViewChildren should get the elements rendered by ngTemplateOutlet.

Minimal reproduction of the problem with instructions
In this plunkr, on app.ts file, you can see a Child component and a Parent component, Parent component has an input property called template1, this accept a TemplateRef that is rendered using ngTemplateOutlet. Parent component has a ViewChildren collection to get the Child components and shows the length of this collection in the end of template.

Well, using this component this way:

    <ng-template #template1>
        <child></child>
        <child></child>
    </ng-template>
    <parent [template1]="template1"></parent>

The Parent component render the two Child components but the length of ViewChildren collection is 0. You can uncomment the line 16 of app.ts file to include a static <child/> component in the Parent template. Then the collection length is 1.

What is the motivation / use case for changing the behavior?
It's would be very useful to pass template to components, rows of grids, lines of lists, ...
In the other hand I have this plunkr in this case I am rendering Child components using a ngFor, ViewChildren works fine.
Since both, ngTemplateOutlet and ngForOf, using viewContainerRef.createEmbeddedView to render the templates ... why works with ngFor and not with ngTemplateOutlet?
I think that the only reason is that Child components come from different component context, but I would like to know it.

Please tell us about your environment:
Windows 10, VisualStudioCode, npm, lite-server

  • Angular version: 4.0.0-rc2

  • Browser: [all]

  • Language: [TypeScript 2.2.1]

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: coreIssues related to the framework runtimecore: queriestype: use-case

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions