question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessing @ViewChild and @ViewChildren of a template declared in a different component than the one where it is host

See original GitHub issue

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:

  • A child component takes a TemplateRef as a @Input() and insert this template through ngTemplateOutlet.
  • A parent component inject the template.
  • The child component can’t access views inside the template.
child.component.html
<ng-container #fromParent [ngTemplateOutlet]="template"></ng-container>
child.component.ts
@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css'],
})
export class ChildComponent implements AfterViewInit {
  @Input() template: TemplateRef<unknown>;
  @ViewChildren(HelloComponent) hello = new QueryList<HelloComponent>();

  ngAfterViewInit() {
    console.log('Has hello', this.hello.length > 0);
  }
}
parent.component.html
<ng-template #tmp> <hello name="{{ name }}"></hello> </ng-template>
<app-child [template]="tmp"> </app-child>

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 same host 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.

image

In this example the ngcontent host id shoulld have been lhp-c2 instead of lhp-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 a ngTemplateOutletContext.

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:18
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
montella1507commented, Aug 18, 2022

So… there is still hope 😃

0reactions
forbreakcommented, Nov 30, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Use ViewChild in Angular to Access a Child ...
In this tutorial, you used ViewChild to access a directive, child component, and a DOM element from a parent component class. If the...
Read more >
Angular @ViewChild: In-Depth Explanation (All Features ...
Conclusion. The @ViewChild decorator allows us to inject into a component class references to elements used inside its template, that's what we ...
Read more >
ViewChild - Angular
Property decorator that configures a view query. The change detector looks for the first element or the directive matching the selector in the...
Read more >
ViewChild from ngTemplateOutlet coming from parent ...
The parent template is part of the parent component host view and hence the ViewChildren in child component will not be able to...
Read more >
Angular ViewChild and ViewChildren | by Mayank Gupta
@ViewChild can be used to access the child component having the template reference userInformation , which represents the child component. Using this ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found